This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Undirected GraphUndirected Graph No Self-LoopsNo Self-Loops Adjacency is symmetricAdjacency is symmetric
This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.
A BC A BC B ACEFB ACEFC ABC ABD ED EE BDFE BDFF BEF BE
A BC A BC B CEFB CEFC C D DD DE BDE BDF EF E
Adjacency Adjacency MatrixMatrix
Adjacency ListAdjacency List Adjacency Adjacency MatrixMatrix
Adjacency ListAdjacency ListThis treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.
Path:Path: length: number of edgeslength: number of edges simple: all vertices distinctsimple: all vertices distinct
Cycle: Cycle: Directed Graph: Directed Graph:
<v<v00,v,v11,...,v,...,vkk > forms cycle if v > forms cycle if v00=v=vkk and and k>=1k>=1
simple cycle: vsimple cycle: v11,v,v22..,v..,vk k also distinctalso distinct self-loop is cycle of length 1self-loop is cycle of length 1
Undirected Graph: Undirected Graph: <v<v00,v,v11,...,v,...,vkk > forms (simple) cycle if > forms (simple) cycle if
vv00=v=vkk and k>=3 and k>=3 simple cycle: vsimple cycle: v11,v,v22..,v..,vk k also distinctalso distinct
B
E
C
FD
A path <A,B,F>path <A,B,F>
B
E
C
FD
A simple cycle simple cycle <E,B,F,E><E,B,F,E>
This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.
This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.
Depth-First Search (DFS) &
Breadth-First Search (BFS)
Depth-First Search (DFS) &
Breadth-First Search (BFS)
ExamplesExamplesVertex Color ChangesVertex Color Changes
Edge ClassificationEdge ClassificationUsing the Results of DFS & BFS Using the Results of DFS & BFS
Running Time AnalysisRunning Time Analysis
Depth-First Search (DFS) Depth-First Search (DFS)
Example: DFS of Directed GraphExample: DFS of Directed Graph
SourceSource: Graph is from : Graph is from Computer Computer Algorithms: Introduction to Design and Algorithms: Introduction to Design and AnalysisAnalysis by Baase and Gelder. by Baase and Gelder.
Vertex is Vertex is WHITEWHITE if it has not yet been if it has not yet been encountered during the search.encountered during the search.
Vertex is Vertex is GRAYGRAY if it has been encountered if it has been encountered but has not yet been fully explored.but has not yet been fully explored.
Vertex is Vertex is BLACK if it has been fully if it has been fully explored.explored.
Edge ClassificationEdge Classification
Each edge of the original graph G is classified during the searchEach edge of the original graph G is classified during the search produces information needed to:produces information needed to:
build DFS or BFS spanning forest of treesbuild DFS or BFS spanning forest of trees detect cycles (DFS) or find shortest paths (BFS)detect cycles (DFS) or find shortest paths (BFS)
When vertex u is being explored, edge e = (u,v) is classified based on When vertex u is being explored, edge e = (u,v) is classified based on the color of v when the edge is the color of v when the edge is first exploredfirst explored:: e is a e is a tree edgetree edge if v is if v is WHITEWHITE [for DFS and BFS] [for DFS and BFS] e is a e is a back edgeback edge if v is if v is GRAYGRAY [for DFS only] [for DFS only]
for DFS this means v is an ancestor of u in the DFS treefor DFS this means v is an ancestor of u in the DFS tree e is a e is a forward edgeforward edge if v is if v is BLACK and [for DFS only] v is a descendent of u in and [for DFS only] v is a descendent of u in
the DFS treethe DFS tree e is a e is a cross edgecross edge if v is if v is BLACK and [for DFS only] there is no ancestor or and [for DFS only] there is no ancestor or
descendent relationship between u and v in the DFS treedescendent relationship between u and v in the DFS tree Note that:Note that:
For BFS we’ll only consider tree edges. For BFS we’ll only consider tree edges. For DFS we consider all 4 edge types.For DFS we consider all 4 edge types. In DFS of an undirected graph, every edge is either a tree edge or a back edge.In DFS of an undirected graph, every edge is either a tree edge or a back edge.
Review problem: TRUE or FALSE?Review problem: TRUE or FALSE? The tree shown below on the right can be a DFS tree for some The tree shown below on the right can be a DFS tree for some
adjacency list representation of the graph shown below on the adjacency list representation of the graph shown below on the
Example: BFS of Directed GraphExample: BFS of Directed Graph
SourceSource: Graph is from : Graph is from Computer Computer Algorithms: Introduction to Design and Algorithms: Introduction to Design and AnalysisAnalysis by Baase and Gelder. by Baase and Gelder.
Shortest path distance from :Shortest path distance from :
A to B = 1A to B = 1 A to E = 2 A to E = 2
A to C = 1A to C = 1 A to G = 2A to G = 2
A to D = 1A to D = 1
A to F = 1A to F = 1
AA
BB
CC
DD
EEFF
GG
TT
TT
TT
TT
TT
TT
AA
FFBB
TTTT TT
DDCC
TT
EE GG
TT TT
Depth-First Search (DFS) &
Breadth-First Search (BFS)
Depth-First Search (DFS) &
Breadth-First Search (BFS)
Using the Results of DFS & BFSUsing the Results of DFS & BFSRunning Time AnalysisRunning Time Analysis
Using the Results of DFS & BFSUsing the Results of DFS & BFS
A directed graph G is acyclic if and only if a A directed graph G is acyclic if and only if a Depth-First Search of G yields no back edges.Depth-First Search of G yields no back edges.
Using DFS to Detect Cycles:Using DFS to Detect Cycles:
Using BFS for Shortest Paths:Using BFS for Shortest Paths:
A Breadth-First Search of G yields shortest path information: A Breadth-First Search of G yields shortest path information:
For each Breadth-First Search tree, the path from its For each Breadth-First Search tree, the path from its root u to a vertex v yields the shortest path from u to v in G.root u to a vertex v yields the shortest path from u to v in G.
see p. 550 of text for proofsee p. 550 of text for proof
Note: DFS can also be used to detect cycles in undirected graphs if Note: DFS can also be used to detect cycles in undirected graphs if notion of cycle is defined appropriately.notion of cycle is defined appropriately.
see p. 534-537 of text for proofsee p. 534-537 of text for proof
From From sourcesource to each reachable vertex to each reachable vertex Record during traversalRecord during traversal
Foundation of many “Foundation of many “shortest pathshortest path” ” algorithmsalgorithms
See DFS, BFS Handout for PseudoCodeSee DFS, BFS Handout for PseudoCode
Depth-First-Search (DFS):Depth-First-Search (DFS): Encountering, finishing times Encountering, finishing times
“ “well-formed” nested (( )( ) ) structurewell-formed” nested (( )( ) ) structure DFS of undirected graph produces only back DFS of undirected graph produces only back
edges or tree edgesedges or tree edges Directed graph is acyclic if and only if DFS Directed graph is acyclic if and only if DFS
yields no back edges yields no back edges
for for unweighted unweighted directeddirected or or undirectedundirected graph G=(V,E) graph G=(V,E)TimeTime: O(|V| + |E|) adj list: O(|V| + |E|) adj list O(|V|O(|V|22) adj matrix) adj matrix
predecessor subgraph = forest of spanning treespredecessor subgraph = forest of spanning trees
Vertex color shows status:Vertex color shows status:not yet encounterednot yet encountered
encountered, but not yet finishedencountered, but not yet finished
finishedfinished
Running Time AnalysisRunning Time Analysis
Key ideas in the analysis are similar for DFS and BFS. In Key ideas in the analysis are similar for DFS and BFS. In both cases both cases we assume an Adjacency List representationwe assume an Adjacency List representation. . Let’s examine DFS.Let’s examine DFS.
Let Let tt be number of DFS trees generated by DFS search be number of DFS trees generated by DFS search Outer loop in DFS(G) executes Outer loop in DFS(G) executes tt times times
each execution contains call: DFS_Visit(G,u)each execution contains call: DFS_Visit(G,u) each such call constructs a DFS tree by visiting each such call constructs a DFS tree by visiting
(recursively) every node reachable from vertex u(recursively) every node reachable from vertex u Time:Time:
Now, let rNow, let rii be the number of vertices in DFS tree i be the number of vertices in DFS tree i Time to construct DFS tree i:Time to construct DFS tree i:
t
i
itreeDFSconstructtotime1
ir
j
itreeDFSinvertexjthAdjList1
][
continued on next slide...continued on next slide...
Running Time AnalysisRunning Time Analysis Total DFS time:Total DFS time: Now, consider this expression for the extreme values of Now, consider this expression for the extreme values of tt::
if t=1, all edges are in one DFS tree and the expression simplifies to O(|E|)if t=1, all edges are in one DFS tree and the expression simplifies to O(|E|) if t=|V|, each vertex is its own (degenerate) DFS tree with no edges so the if t=|V|, each vertex is its own (degenerate) DFS tree with no edges so the
expression simplifies to O(|V|)expression simplifies to O(|V|) O(|V|+|E|) is therefore an upper bound on the time for the extreme casesO(|V|+|E|) is therefore an upper bound on the time for the extreme cases
For values of t in between 1 and |V| we have these contributions to For values of t in between 1 and |V| we have these contributions to running time:running time:
1 for each vertex that is its own (degenerate) DFS tree with no edges 1 for each vertex that is its own (degenerate) DFS tree with no edges upper bound on this total is O(|V|)upper bound on this total is O(|V|)
|AdjList[u]| for each vertex u that is a node of a non-degenerate DFS tree|AdjList[u]| for each vertex u that is a node of a non-degenerate DFS tree upper bound on this total is O(|E|)upper bound on this total is O(|E|)
Total time for values of t in between 1 and |V| is therefore also O(|V|+|E|)Total time for values of t in between 1 and |V| is therefore also O(|V|+|E|)
t
i
r
j
i
itreeDFSinvertexjthAdjList1
1
][
|)||(| EVO Total time=Total time=
Note that for an Note that for an Adjacency MatrixAdjacency Matrix representation, we would need to scan an entire representation, we would need to scan an entire matrix row (containing |V| entries) each time we examined the vertices adjacent to a matrix row (containing |V| entries) each time we examined the vertices adjacent to a vertex. This would make the running time O(|V|vertex. This would make the running time O(|V|22) instead of O(|V|+|E|).) instead of O(|V|+|E|).
A A Directed Acyclic Graph often abbreviated DAGDirected Acyclic Graph often abbreviated DAG DAGs used in many applications to indicate DAGs used in many applications to indicate
precedence among events. precedence among events. If DFS of a directed graph yields no back edges, If DFS of a directed graph yields no back edges,
then the graph contains no cycles then the graph contains no cycles [Lemma 22.11 in text][Lemma 22.11 in text]
AA
BB
CC
DD
EEFF
GG
This graph has more than one cycle.This graph has more than one cycle.
Can you find them all?Can you find them all?
AA
BB
CC
DD
EEFF
GG
This graph has no cycles, This graph has no cycles, so it is a DAG.so it is a DAG.
A A topological sorttopological sort of a DAG Gof a DAG G = (V, E) is a = (V, E) is a linear ordering of all its vertices such that if linear ordering of all its vertices such that if G contains an edge (u, v), then u appears G contains an edge (u, v), then u appears before v in the ordering. before v in the ordering. If the graph is not acyclic, then no linear ordering is If the graph is not acyclic, then no linear ordering is
possible. possible. A topological sort of a graph can be viewed as an A topological sort of a graph can be viewed as an
ordering of its vertices along a horizontal line so that all ordering of its vertices along a horizontal line so that all directed edges go from left to right. directed edges go from left to right.
Topological sorting is thus different from the usual kind Topological sorting is thus different from the usual kind of "sorting" .of "sorting" .
source: 91.503 textbook Cormen et al.source: 91.503 textbook Cormen et al.
TOPOLOGICAL-SORT(G)TOPOLOGICAL-SORT(G)1 DFS(G) computes “finishing times” for each vertex1 DFS(G) computes “finishing times” for each vertex2 as each vertex is finished, insert it onto front of list2 as each vertex is finished, insert it onto front of list3 return list3 return list
for Directed, Acyclic Graph (DAG) G=(V,E)
Produces linear ordering of vertices.Produces linear ordering of vertices.For edge (u,v), u is ordered before v.For edge (u,v), u is ordered before v.
Topological SortTopological Sort
The following algorithm topologically sorts The following algorithm topologically sorts a DAG:a DAG:
TOPOLOGICAL-SORT(G)TOPOLOGICAL-SORT(G) call DFS(G) to compute finishing times f[v] for call DFS(G) to compute finishing times f[v] for
each vertex v (this is equal to the order in each vertex v (this is equal to the order in which vertices change color from gray to black)which vertices change color from gray to black)
as each vertex is finished (turns black), insert it as each vertex is finished (turns black), insert it onto the front of a linked listonto the front of a linked list
return the linked list of verticesreturn the linked list of vertices
ExampleExample
For this DAG:For this DAG:
DFS produces this result:DFS produces this result: this contains 2 DFS treesthis contains 2 DFS trees
Vertices are blackened in the Vertices are blackened in the following order:following order: C, B, F, A, D, E , GC, B, F, A, D, E , G
AA
BB
CC
DD
EEFF
GG
AA
BB
CC
DD
EEFF
GGTT
TT
TT
TT
TT
FF
CC CC
CC
CC
ExampleExample
Vertices are added to Vertices are added to frontfront of a linked list in the of a linked list in the blackening order.blackening order.
Final result is shown belowFinal result is shown below Note that all tree edges and non-tree edges point Note that all tree edges and non-tree edges point
to the right to the right
AA BB CCDDEE FFGG TT
FF
CC TT TT
TTCC
TT CC
CC
Topological SortTopological Sort
We can perform a topological sort in time We can perform a topological sort in time (V + E), since depth-first search takes (V + E), since depth-first search takes (V + E) time and it takes 0(1) time to (V + E) time and it takes 0(1) time to insert each of the |V| vertices onto the front insert each of the |V| vertices onto the front of the linked list.of the linked list.
Topological SortTopological Sort
Theorem 23.11: Theorem 23.11: TOPOLOGICAL-SORT(G) produces a TOPOLOGICAL-SORT(G) produces a topological sort of a directed acyclic graph G.topological sort of a directed acyclic graph G.
Proof: Proof: Suppose that DFS is run on a given dag G = (V, E) to Suppose that DFS is run on a given dag G = (V, E) to determine finishing times for its vertices. It suffices to show determine finishing times for its vertices. It suffices to show that for any pair of distinct vertices u,v Î V, if there is an edge in that for any pair of distinct vertices u,v Î V, if there is an edge in G from u to v, then f[v] < f[u]. Consider any edge (u,v) G from u to v, then f[v] < f[u]. Consider any edge (u,v) explored by DFS(G). When this edge is explored, v cannot be explored by DFS(G). When this edge is explored, v cannot be gray, since then v would be an ancestor of u and (u,v) would be gray, since then v would be an ancestor of u and (u,v) would be a back edge, contradicting Lemma 22.11. Therefore, v must be a back edge, contradicting Lemma 22.11. Therefore, v must be either white or black. If v is white, it becomes a descendant of either white or black. If v is white, it becomes a descendant of u, and so f[v] < f[u]. If v is black, then f[v] < f[u] as well. Thus, u, and so f[v] < f[u]. If v is black, then f[v] < f[u] as well. Thus, for any edge (u,v) in the dag, we have f[v] < f[u], proving the for any edge (u,v) in the dag, we have f[v] < f[u], proving the theorem.theorem.
Chapter 23Minimum Spanning Trees
Chapter 23Minimum Spanning Trees
Kruskal Kruskal PrimPrim
[Source: Cormen et al. textbook except where noted][Source: Cormen et al. textbook except where noted]
source: 91.503 textbook Cormen et al.source: 91.503 textbook Cormen et al.
for Undirected, Connected, Weighted Graph
G=(V,E)
Produces minimum weight tree of Produces minimum weight tree of edges that includes every vertex.edges that includes every vertex.
Invariant: Minimum weight spanning forest
Becomes single tree at end
Invariant: Minimum weight tree
Spans all vertices at end
TimeTime: O(|E|: O(|E|lg|E|) given lg|E|) given fast FIND-fast FIND-SET, SET, UNIONUNION
TimeTime: O(|E|: O(|E|lg|V|) = O(|lg|V|) = O(|E|lg|E|) E|lg|E|) slightly slightly faster with faster with fast priority fast priority queuequeue
Minimum Spanning TreesMinimum Spanning Trees
Review problem:Review problem: For the undirected, weighted graph below, show 2 different For the undirected, weighted graph below, show 2 different
Minimum Spanning Trees. Draw each using one of the 2 graph Minimum Spanning Trees. Draw each using one of the 2 graph copies below. Thicken an edge to make it part of a spanning copies below. Thicken an edge to make it part of a spanning tree. What is the sum of the edge weights for each of your tree. What is the sum of the edge weights for each of your Minimum Spanning Trees?Minimum Spanning Trees?
A B
CD
E
F
G
2
2
1 1
3
4
4
5 6
68
7
Chapter 24Shortest Paths
Chapter 24Shortest Paths
DijkstraDijkstra
[Source: Cormen et al. textbook except where noted][Source: Cormen et al. textbook except where noted]
BFS as a Basis for Shortest Path AlgorithmsBFS as a Basis for Shortest Path Algorithms
ProblemProblem: Given a vertex : Given a vertex u, find the shortest path in u, find the shortest path in G from u to each vertex.G from u to each vertex.
SolutionSolution: BFS : BFS starting at u. Full starting at u. Full BFS tree.BFS tree.
source: based on Sedgewick, Graph Algorithmssource: based on Sedgewick, Graph Algorithms
BFSBFSfor unweighted, for unweighted, undirected graph G=(V,E)undirected graph G=(V,E)
““Relax” a constraint to try to improve solutionRelax” a constraint to try to improve solution ““Rubber band” analogy Rubber band” analogy [Sedgewick][Sedgewick]
Relaxation of an Edge (u,v):Relaxation of an Edge (u,v): test if shortest path to v test if shortest path to v [found so far][found so far] can be improved can be improved
Review problem:Review problem: For the directed, weighted graph below, find the shortest
path that begins at vertex A and ends at vertex F. List the vertices in the order that they appear on that path. What is the sum of the edge weights of that path?