Topological sort and Connected components in directed graph

Since I’m reading about algorithms, I figured I’d review topological sort and Kosaraju’s algorithm as well.

To sort vertices in a directed graph topologically is to arrange them into a sequence so that no vertex has an edge pointing to a vertex earlier in that sequence. Of course, this requires the graph to be DAG (directed acyclic graph).

The algorithm is simply to find the reverse postorder in DFS (depth first search) of the vertices. This is implemented by pushing a vertex into a stack after its DFS call completes. The order in which vertices are popped out of the stack is the graph’s reverse postorder. The proof is described below:

We need to prove that for every edge from s to v, s would appear before v in reverse postorder. There are several conditions:

  1. v is called and returned before the call to s. In this case, s is pushed to the stack later than v, hence s appears before v.
  2. v is called later than s but returned before s returns. The former conclusion also holds.
  3. v is called later than s has returned. This case is impossible. Since s has an edge to v, v has to be called somewhere earlier than s returns.

Therefore, our algorithm can give us a topological sort.

Next, we use a similar way to look for connected components in DAG. A connected component is defined as a subgraph in which all vertices have a way to reach each other. This algorithm is not so obvious: first get a topological order of the reverse of the graph G – Gr, then use the order to run DFS in G. All vertices reached by one call belong to a connected component, so we can mark them.

The proof also needs some brain.

Consider the second step of the algorithm. First, it is obvious every connected component edge is called in the second step, duh. Then we prove from the other side. Say we have a vertex v that is reached by calling DFS on s. We’re gonna prove there is an edge from v to s as well.

From the way we generated the order, s appears before v in reverse postorder of Gr. Now consider Gr, we have several conditions:

  1. the call to v ends before the call to s starts. This is impossible because v has an edge to s in Gr.
  2. the call to v starts after the call to s, and ends before s ends. This means there is an edge from s to v in Gr, which then proves that in G there is an edge from v to s. Connected component it is.
  3. the call to v starts after the call to s ends. Impossible because s appears before v in reverse postorder of Gr.

Therefore, Kosoraju’s algorithm is correct in finding connected components of DAG.

I appreciate the brilliance of the algorithms. It’s interesting because they don’t seem so hard to me now but I spent days to understand the proof the first time I read about them. I’m reading divide-and-conquer next. I’m hoping all the complicated recursions don’t play with my mind too much.

2,486 thoughts on “Topological sort and Connected components in directed graph”

  1. Thanks for the sensible critique. Me and my neighbor were just preparing to do a little research about this. We got a grab a book from our local library but I think I learned more clear from this post. I am very glad to see such excellent information being shared freely out there.

  2. Ellanse洢蓮絲(依戀詩)是一款荷蘭與英國共同研發的的新型真皮填充劑,是由30的25-50微米(µm)的聚己內酯(polycaprolactone, PCL)完美微型正圓晶球,以及70的PBS-生物降解材料(carboxymethylcellulose, CMC)製成的凝膠體,這些成分都是通過FDA(美國食品藥品監督管理局)和歐洲CE認證的安全成分,在人體內水分和二氧化碳作用下可以完全被分解吸收和排出的安全物質,對人體不會產生過敏反應,因此治療前不需經過敏檢測,在使用上幾乎不產生副作用。

  3. StriVectin 皺效奇蹟 商品一覽 UrCosme (@cosme TAIWAN) 找品牌 以UrCosme指數高至低排序第1頁 StriVectin 皺效奇蹟 化妝品商品一覽,提供 StriVectin 皺效奇蹟的產品列表,以UrCosme指數高至低排列,共42件,StriVectin 皺效奇蹟的搜尋結果之第1頁

  4. 這對好兄弟同場現身真的太搶眼了!你要選彭于晏還是崔始源? Marie Claire (HK) Edition 我們常常都說閨密團,總是在說女生們,感覺都沒有在說男生們。其實娛樂圈有很多好兄弟,單是彭于晏就有不少好朋友了,例如是剛剛成為爸爸的余文樂,以及韓國型男崔始源。 彭于晏與崔始源結緣於2015年,當時因

  5. – canadian viagra cheap – amoxicillin – Buy Propecia – Buy Prednisolone No Prescription – cephalexin – lexapro – viagra – orlistat diet pills – buying acyclovir – tetracycline

  6. neweddrugs.comhttp://www.neweddrugs.comed drugs onlinenew ed drugsnew ed drugsnew ed drugsnew ed drugsnew ed drugsnew ed drugsnew ed drugsnew ed drugsed drugsed drugsed drugsed drugsnew ed medsnew ed drugsnew ed medsed drugs onlineed drugs onlineed drugs and indiainternet based drug storesed drugs online from canadaed drugs indiahow to buy ed drugs from canadaed drug shoped drugs from indiabest prices on ed drugsdo generic ed drugs really workcanadian ed drugs onlinedrugs for erectile dysfunction new drugs for erectile dysfunction impotene drugs drugs for impotence erectile dysfunction drugs generic impotence drugs new impotence drugs ed drugs generic best ed drugs best drugs for ed erection drugs pharmacy online generic drugs for erection generic erection drugs buy impotence drugs drugs for impotence online best ed drugs best impotence drugs best drug for ed best ed drugsbest drugs for edbest drug for edbest ed drugtop ed drugtop ed drugstop drug for edtop drugs for ede d drugswhat are the best generic ed drugsed drugserectile dysfunction drugsed drugs onlinegeneric ed drugscheap erectile dysfunction drugs-canadacanadian erectile dysfunction drugsgeneric drugs for erectile dysfunctioned drug cost comparisonbuy erectile dysfunction drugscheap ed drugsbest erectile dysfunction drugs for sale onlineerectile dysfunction drugs onlineerectile dysfunction drugs canadaed drugbest ed druggeneric name for ED drugsbuy erectile dysfunction drugs onlineerectile dysfunction drugs comparisonimpotence drugs online online pharmacy canada viagra brand at low price best place to buy viagra online cialis prices canadian pharmacies online on line pharmacy with no prescription canadian pharmacy online no script canadian pharmacy cialis cialis online no prescription brand viagra for sale

  7. Pingback: buy biaxin
  8. Pingback: buy ceftin
  9. Pingback: buy chloromycetin
  10. Yeezys
    Yeezy Shoes
    Yeezy Shoes
    Yeezy Boost 350
    Yeezy Boost 350
    Yeezy Boost 350 V2 Blue Tint
    Yeezy 500 Utility Black
    Yeezy 500
    Nike Air VaporMax
    Salomon UK
    Salomon Shoes
    Off White Jordan 1
    Nike VaporMax
    Nike React Element 87
    Nike Element 87
    Nike Plus
    Nike Outlet–
    Nike Outlet Store Online Shopping
    Nike Outlet Store
    NBA Jerseys
    Air Max 97
    Air Max 2017
    Air Jordan Shoes
    Jordan 11 Concord
    Cheap Yeezy Shoes
    Wholesale Cheap NBA Jerseys
    Birkenstock Sandals UK
    Basketball Jersey
    Balenciaga UK
    Balenciaga UK
    Balenciaga Trainers
    Nike Air Max 270
    Adidas Yeezys
    Adidas Yeezy Shoes

  11. Pingback: buy biaxin online
  12. Pingback: buy ceftin online
  13. Pingback: buy cialis
  14. Pingback: buy cordarone
  15. Pingback: buy viagra
  16. Pingback: cialis 20mg
  17. Pingback: generic cialis
  18. skypharmacy sky pharmacy canada rhine inc in india cheap ed meds online ed medicines generic for erectile dysfunction ed medications best ed medication what are the best generic ed drugs online erectile dysfunction medications ed medications compared cheap ed medication ed medications online ed drugs online best erectile dysfunction medication in canada ed drugs meds for ed erectile dysfunction medications sold in canada erectile dysfunction drugs generic ed drugs cheap erectile dysfunction drugs-canada canadian erectile dysfunction drugs ED medication order erectile dysfunction medication generic drugs for erectile dysfunction erectile meds ed drug cost comparison buy erectile dysfunction drugs generic ed medication erectile dysfunction medication prices ed meds on line cheap ed drugs best erectile dysfunction drugs for sale online erectile dysfunction drugs online meds for erectile dysfunction generic ed meds erectile dysfunction drugs canada ed drug best ed drug best deal on ED meds cheap ed meds medication from canada ed erectile dysfunction generic name for ED drugs buy erectile dysfunction drugs online cheap erectile dysfunction medication generic erectile dysfunction medications erectile dysfunction drugs comparison impotence drugs online common drugs for ed generic impotence drugs ed meds cost effective ed drugs canadian erectile dysfunction drugs ed medsbest ed drug cheapest cialis 20mg northwest pharmacy canada nizagara 100 antibiotics for sale 24 hour delivery pharmacy rx one