Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and...
Transcript of Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and...
![Page 1: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/1.jpg)
Strongly Connected Componentsand Breadth‐First Search
CSE 101: Design and Analysis of AlgorithmsLecture 4
![Page 2: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/2.jpg)
CSE 101: Design and analysis of algorithms
• Strongly connected components and breadth‐first search– Reading: Sections 3.4, 4.1, 4.2, and 4.3
• Homework 1 due today, 11:59 PM• Homework 2 will be assigned today
– Due Oct 16, 11:59 PM
CSE 101, Fall 2018 2
![Page 3: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/3.jpg)
Depth‐first search on directed graphs
A C
B D
E G
F H
CSE 101, Fall 2018 3Based on slides courtesy of Miles Jones
![Page 4: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/4.jpg)
Depth‐first search on directed graphs (reverse alphabetical order)
A C
B D
E G
F H
CSE 101, Fall 2018 4
![Page 5: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/5.jpg)
• Tree edge: solid edge included in the depth‐first search output tree
• Back edge: leads to an ancestor• Forward edge: leads to a descendent• Cross edge: leads to neither ancestor or descendent
• Note that back edge is slightly different in directed and undirected graphs
Edge types (directed graph)
CSE 101, Fall 2018 5
![Page 6: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/6.jpg)
Edge types and pre/post numbers• The different types of edges can be determined from the pre/post numbers for the edge – is a tree/forward edge then
– is a back edge then
– is a cross edge then
CSE 101, Fall 2018 6
![Page 7: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/7.jpg)
Cycles in directed graphs• A cycle in a directed graph is a path that starts and ends with the same vertex
CSE 101, Fall 2018 7
![Page 8: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/8.jpg)
A directed graph has a cycle if and only if its depth‐first search reveals a back edge
• Proof: – Suppose G has a cycle
CSE 101, Fall 2018 8
![Page 9: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/9.jpg)
A directed graph has a cycle if and only if its depth‐first search reveals a back edge
• Proof: – Suppose G has a cycle
– Suppose is the first vertex to be discovered• What does that mean about ?
CSE 101, Fall 2018 9
![Page 10: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/10.jpg)
A directed graph has a cycle if and only if its depth‐first search reveals a back edge
• Proof: – Suppose G has a cycle
– Suppose is the first vertex to be discovered• What does that mean about ?
– It is the vertex with the lowest pre number
CSE 101, Fall 2018 10
![Page 11: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/11.jpg)
A directed graph has a cycle if and only if its depth‐first search reveals a back edge
• Proof: – Suppose G has a cycle
– Suppose is the first vertex to be discovered• All other are reachable from it and therefore, they are all descendants in the depth‐first search tree
CSE 101, Fall 2018 11
![Page 12: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/12.jpg)
A directed graph has a cycle if and only if its depth‐first search reveals a back edge
• Proof: – Suppose G has a cycle
– Suppose is the first vertex to be discovered• All other are reachable from it and therefore, they are all descendants in the depth‐first search tree
– Therefore the edge is a back edge CSE 101, Fall 2018 12
![Page 13: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/13.jpg)
A directed graph has a cycle if and only if its depth‐first search reveals a back edge
• Proof: – Suppose is a back edge
CSE 101, Fall 2018 13
![Page 14: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/14.jpg)
A directed graph has a cycle if and only if its depth‐first search reveals a back edge
• Proof: – Suppose is a back edge
• is a descendant of in the depth‐first search output tree
• There is a path from to
CSE 101, Fall 2018 14
![Page 15: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/15.jpg)
A directed graph has a cycle if and only if its depth‐first search reveals a back edge
• Proof: – Suppose is a back edge– Then by definition is a ancestor of so there is a path from to in the depth‐first search output tree
CSE 101, Fall 2018 15
![Page 16: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/16.jpg)
A directed graph has a cycle if and only if its depth‐first search reveals a back edge
• Proof: – Suppose is a back edge– Then by definition is a ancestor of so there is a path from to in the depth‐first search output tree
– Along with the back edge, this path completes a cycle
CSE 101, Fall 2018 16
![Page 17: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/17.jpg)
Directed acyclic graphs (dags)• A directed graph without a cycle is called acyclic• We can test whether a graph is acyclic with depth‐first search– Step 1: perform depth‐first search on the graph
• Keep track of pre and post numbers– Step 2: test each edge to see if it is a back edge
• If (i.e., , is back edge), then return false
CSE 101, Fall 2018 17
![Page 18: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/18.jpg)
Linearization of dags• Is it possible to order the vertices such that all edges go in only one direction?
• For what types of dags is this possible?• How do we find such an ordering?
CSE 101, Fall 2018 18
![Page 19: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/19.jpg)
Directed acyclic graphs (dags)• Property: every edge in a dag goes from a higher post number to lower post number
• Proof: suppose is an edge in a dag, then it cannot be a back edge. Therefore it can only be a forward edge/tree edge or a cross edge.– Both of which have the property that
CSE 101, Fall 2018 19
![Page 20: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/20.jpg)
Edge types and pre/post numbers• The different types of edges can be determined from the pre/post numbers for the edge – is a tree/forward edge then
– is a back edge then
– is a cross edge then
CSE 101, Fall 2018 20
![Page 21: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/21.jpg)
Linearization of a dag• Since we know that edges go in the direction of decreasing post numbers, if we order the vertices by decreasing post numbers then we will have a linearization
procedure linearize(a dag G=(V,E))run DFS(G)return list of vertices in decreasing order of post numbers.
CSE 101, Fall 2018 21
![Page 22: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/22.jpg)
Linearization of a dagprocedure linearize(a dag G=(V,E))run DFS(G) Keep track of pre and post numbers O(n + m)return list of vertices in decreasing order of post numbers. O(n log n)Total time: O(n + m + n log n)
Alternatively, add vertices to an output list as you assign post numbers, then reverse list O(n + m)
CSE 101, Fall 2018 22
n = |V|m = |E|
![Page 23: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/23.jpg)
Sources and sinks• Since all dags can be linearized that means the first vertex in the ordering does not have any edges coming in and the last vertex does not have any edges going out
• Definitions– A vertex with no incoming edges is called a source– A vertex with no outgoing edges is called a sink
• Property: all dags have at least one source and one sink
CSE 101, Fall 2018 23
![Page 24: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/24.jpg)
Pre and post numbers in dags• The vertex with the highest post number is always?
• The vertex with the lowest post number is always?
CSE 101, Fall 2018 24
![Page 25: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/25.jpg)
Pre and post numbers in dags• The vertex with the highest post number is always?– Source
• The vertex with the lowest post number is always?– Sink
CSE 101, Fall 2018 25
![Page 26: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/26.jpg)
Example
CSE 101, Fall 2018 26
![Page 27: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/27.jpg)
Example
CSE 101, Fall 2018 27
![Page 28: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/28.jpg)
Strongly connected vertices• Two vertices and in a
directed graph are strongly connected if there exists a path from to and a path from to
• Which vertices are strongly connected to J?
CSE 101, Fall 2018 28
A B
CD
E
FG
H I
J
KL
M
![Page 29: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/29.jpg)
Strongly connected vertices• Two vertices and in a
directed graph are strongly connected if there exists a path from to and a path from to
• Which vertices are strongly connected to J?K‐L‐M‐J
CSE 101, Fall 2018 29
A B
CD
E
FG
H I
J
KL
M
![Page 30: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/30.jpg)
Strongly connected graph• A graph is called strongly
connected if for each pair of vertices v,u there is a path from v to u and a path from u to v
• Is this a strongly connected graph?
CSE 101, Fall 2018 30
A B
CD
E
FG
H I
J
KL
M
![Page 31: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/31.jpg)
Strongly connected graph• A graph is called strongly
connected if for each pair of vertices v,u there is a path from v to u and a path from u to v
• Is this a strongly connected graph?– No
CSE 101, Fall 2018 31
A B
CD
E
FG
H I
J
KL
M
![Page 32: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/32.jpg)
Strongly connected components• Consider the relation if is strongly connected to
• Then is an equivalence relation. It is reflexive, symmetric, and transitive.
• So partitions , the set of vertices into equivalence classes
• These equivalence classes are called strongly connected components
CSE 101, Fall 2018 32
![Page 33: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/33.jpg)
Strongly connected components• What are the strongly connected components of this graph?
CSE 101, Fall 2018 33
A B
CD
E
FG
H I
J
KL
M
![Page 34: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/34.jpg)
Strongly connected components
CSE 101, Fall 2018 34
A B
CD
E
FG
H I
J
KL
M
• What are the strongly connected components of this graph?
![Page 35: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/35.jpg)
Strongly connected components as vertices
CSE 101, Fall 2018 35
A B
CD
E
FG
H I
J
KL
M
AB,C,F,I D
G
J,K,L,M
E
H
![Page 36: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/36.jpg)
Strongly connected components• Property: every directed graph is a dag of its strongly connected components
• Some strongly connected components are sinks and some are sources
CSE 101, Fall 2018 36
![Page 37: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/37.jpg)
Strongly connected components as vertices
CSE 101, Fall 2018 37
A B
CD
E
FG
H I
J
KL
M
AB,C,F,I D
G
J,K,L,M
E
H
Sink
Source
Source
![Page 38: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/38.jpg)
Decomposition of a directed graph• There is a linear time algorithm that decomposes a directed graph into its strongly connected components
• If explore is performed on a vertex , then it will visit only the vertices that are reachable by
• What vertices will be visited when explore is performed on if it is in a sink strongly connected component?– Only the vertices in that strongly connected component will be reached
CSE 101, Fall 2018 38
![Page 39: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/39.jpg)
Decomposition of a directed graph• There is a linear time algorithm that decomposes a directed graph into its strongly connected components
• If explore is performed on a vertex , then it will visit only the vertices that are reachable by
• What vertices will be visited when explore is performed on if it is in a sink strongly connected component?– Only the vertices in that strongly connected component will be reached
CSE 101, Fall 2018 39
![Page 40: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/40.jpg)
Sink strongly connected components• If explore is performed on a vertex that is in a sink strongly connected component, then only the vertices from that strongly connected component will be visited
• This suggests a way to look for strongly connected components– Start explore on a vertex in a sink strongly connected component and visit its strongly connected component
– Remove the sink strongly connected component from the graph and repeat
CSE 101, Fall 2018 40
![Page 41: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/41.jpg)
Source strongly connected components• Ideally, we would like to find a vertex in a sink strongly
connected component– There is not a direct way to do this
• However, there is a way to find a vertex in a source strongly connected component– The vertex with the highest post number in any depth‐first
search output tree belongs to a source strongly connected component
• Note: the vertex with the lowest post number in a depth‐first search output does not necessarily belong to a sink strongly connected component
CSE 101, Fall 2018 41
![Page 42: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/42.jpg)
Example where lowest post number is not in a sink
CSE 101, Fall 2018 42
AC
BD
E
F
G
![Page 43: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/43.jpg)
Example where lowest post number is not in a sink
AC
BD
E
F
G
CSE 101, Fall 2018 43
![Page 44: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/44.jpg)
Source strongly connected components
• The vertex with the highest post number in any depth‐first search output tree belongs to a source strongly connected component
• To prove this, we will state a more general property– If and are strongly connected components and there is an edge from a vertex in to a vertex in then the highest post number in is greater than the highest post number in
CSE 101, Fall 2018 44
![Page 45: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/45.jpg)
Proof• Case 1: depth‐first search searches before ′:
– At some point depth‐first search will cross into C’ and visit every edge in C’, then it will retrace its steps until it gets back to the first node in C it started with and assign it the highest post number
CSE 101, Fall 2018 45
C C’
![Page 46: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/46.jpg)
Proof• Case 2: depth‐first search searches ′ before :
– Depth‐first search will visit all vertices of C’ before getting stuck and assign a post number to all vertices of C’, then it will visit some vertex of C later and assign post numbers to those vertices
CSE 101, Fall 2018 46
C C’
![Page 47: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/47.jpg)
Corollary• The strongly connected components can be linearized by arranging them in decreasing order of their highest post numbers
CSE 101, Fall 2018 47
![Page 48: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/48.jpg)
Finding sink strongly connected components
• Given a graph , let be the reverse graph of– Then, the sources of are the sinks of
• If we perform depth‐first search on , then the vertex with the highest post number is in a source. This means that this vertex will be in a sink of .
• Start with this vertex and explore the strongly connected component
• Then, the vertex with the next highest post number in is in the next strongly connected component in linear order, so start with that one next
CSE 101, Fall 2018 48
![Page 49: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/49.jpg)
Decomposing a graph into its strongly connected components
• Generate • Run depth‐first search on , keeping track of the post numbers
• Run depth‐first search on and order the vertices in decreasing order of the post numbers from the previous step. Every time depth‐first search increments cc, you have found a new strongly connected component.
CSE 101, Fall 2018 49
![Page 50: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/50.jpg)
A B
CD
E
FG
H I
J
KL
M
Example
CSE 101, Fall 2018 50
![Page 51: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/51.jpg)
A B
CD
E
FG
H I
J
KL
M
Example
CSE 101, Fall 2018 51
![Page 52: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/52.jpg)
A B
CD
E
FG
H I
J
KL
M
Example
CSE 101, Fall 2018 52
![Page 53: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/53.jpg)
Decomposing a graph into its strongly connected components
• Generate • Run depth‐first search on , keeping track of the post
numbers• Run depth‐first search on and order the vertices in
decreasing order of the post numbers from the previous step. Every time depth‐first search increments cc, you have found a new strongly connected component.
• How long does this take?
CSE 101, Fall 2018 53
![Page 54: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/54.jpg)
Decomposing a graph into its strongly connected components
• Generate O(n + m)• Run depth‐first search on , keeping track of the post
numbers O(n + m)• Run depth‐first search on and order the vertices in
decreasing order of the post numbers from the previous step. Every time depth‐first search increments cc, you have found a new strongly connected component. O(n + m)
• How long does this take? O(n + m)
CSE 101, Fall 2018 54
![Page 55: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/55.jpg)
Depth‐first search is good for• Find what vertices can be reached by a given vertex• Divide an undirected graph into connected components
• Find cycles in graphs (directed or undirected)• Find sinks and sources in dags• Topologically sort a dag• Make a directed graph into a dag of its strongly connected components
CSE 101, Fall 2018 55
![Page 56: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/56.jpg)
Depth‐first search is not good for• Finding shortest distances between vertices
CSE 101, Fall 2018 56
![Page 57: Strongly Connected Components and Breadth First Search · 2018. 10. 10. · CSE 101: Design and analysis of algorithms • Strongly connected components and breadth‐ first search](https://reader035.fdocuments.in/reader035/viewer/2022071105/5fdf2e78f448f87792226af7/html5/thumbnails/57.jpg)
Next lecture• Dijkstra’s algorithm and priority queue implementations– Reading: Sections 4.4 and 4.5
CSE 101, Fall 2018 57