Download presentation
Presentation is loading. Please wait.
Published byMarshall Richard Modified over 9 years ago
1
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e f g A, B, C, D are vertices A Vertex is a single piece of information held in the graph (like nodes in a list or tree) An Edge connects two vertices a,b,c,d,e,f,g are edges A vertex can also have an edge to itself (a loop)
2
11 Copyright © William C. Cheng C ABAB D Data Structures - CSCI 102 Graph Terminology Graphs are defined by a set of vertices V and a set of edges E G = (V, E) V(G) = { A, B, C, D } E(G) = { (A,B), (A,C), (C,D), (D,B), (A,D) }
3
12 Copyright © William C. Cheng C ABAB D Data Structures - CSCI 102 Graph Terminology Undirected Graphs Edges have no concept of "direction" We can traverse any edge in either direction Moving from C to A is permissible Moving from A to C is permissible
4
13 Copyright © William C. Cheng C ABAB D Data Structures - CSCI 102 Graph Terminology Directed Graphs Edges are directional We can only traverse an edge in one direction Moving A to C is permissible Moving C to A is not permissible
5
14 Copyright © William C. Cheng C A B D Data Structures - CSCI 102 Graph Terminology Weighted Graphs Edges can have an associated weight directed Implies a cost Weight graphs can be undirected or Example Vertices are cities Edges are roads and Edge Weights are the distances between cities 2 4 9 23 4 5
6
C AD Data Structures - CSCI 102 Graph Terminology Two vertices are adjacent if they have an edge connecting them C & A are adjacent C & B are not adjacent There is a path from X to Y if it is possible to reach vertex X from vertex Y There is a path from every vertex to every other vertex in this example B 15 Copyright © William C. Cheng
7
16 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology Cycles A path is considered a cycle if it has the same starting and ending vertex B → A → D → B is an example of a cycle C ABAB D
8
Linked Lists 9 Data Structures - CSCI 102 Copyright © William C. Cheng Graphs Each node knows the location of exactly one other node Lists can’t have cycles Binary Trees Graphs Each node knows the location of exactly two other nodes Trees can’t have cycles Each node knows the location of N other nodes Graphs can have cycles
9
Data Structures - CSCI 102 Graph How would you represent a graph in code? Adjacency Matrix C A B 17 Copyright © William C. Cheng D Adjacency List More terminology E is the number of edges in the graph V is the number of vertices in the graph
10
Adjacency Matrix 18 Data Structures - CSCI 102 Copyright © William C. Cheng Graph G is a graph with N vertices The adjacency matrix of G is an N by N matrix Entry (i,j) in the matrix is 1 if there is an edge from vertex V i to vertex V j Otherwise entry (i,j) is 0
11
AG =AG = 19 Copyright © William C. Cheng Data Structures - CSCI 102 Adjacency Matrix 2 15 4 3 V3V3 V1V2V1V2 V4V5V4V5 V 1 V 2 V 3 V 4 V 5
12
Data Structures - CSCI 102 Adjacency Matrix 2 15 4 3 Vertex 1 connects to 2 and 5 20 Copyright © William C. Cheng AG =AG =V3V3 V2V2 V1V1 V4V5V4V5 V 1 V 2 V 3 V 4 V 5 01001
13
Data Structures - CSCI 102 Adjacency Matrix 2 15 4 3 AG =AG =V3V3 V1V2V1V2 V4V5V4V5 V 1 V 2 V 3 V 4 V 5 0101 1010 0101 0101 1111 Vertex 1 connects to 2 and 5 Vertex 2 connects to 1, 3, 4, & 5 21 Copyright © William C. Cheng
14
Data Structures - CSCI 102 Adjacency Matrix 2 15 4 3 AG =AG =V3V3 V1V2V1V2 V4V5V4V5 V 1 V 2 V 3 V 4 V 5 01010 0101 1010 0101 0101 1111 Vertex 1 connects to 2 and 5 Vertex 2 connects to 1, 3, 4, & 5 Vertex 3 connects to 2 & 4 22 Copyright © William C. Cheng
15
Data Structures - CSCI 102 Adjacency Matrix 2 15 4 3 AG =AG =V3V3 V1V2V1V2 V4V4 V5V5 V 1 V 2 V 3 V 4 V 5 11001 01010 0101 1010 0101 0101 1111 Vertex 1 connects to 2 and 5 Vertex 2 connects to 1, 3, 4, & 5 Vertex 3 connects to 2 & 4 Vertex 4 connects to 2, 3 & 5 23 Copyright © William C. Cheng
16
24 Data Structures - CSCI 102 Adjacency Matrix 2 15 4 3 AG =AG =V3V3 V1V2V1V2 V4V5V4V5 1010 1111 0101 0101 1010 01010 0101 1010 0101 0101 1111 V 1 V 2 V 3 V 4 V 5 Vertex 1 connects to 2 and 5 Vertex 2 connects to 1, 3, 4, & 5 Vertex 3 connects to 2 & 4 Vertex 4 connects to 2, 3 & 5 Vertex 5 connects to 1, 2 & 4 Copyright © William C. Cheng
17
Data Structures - CSCI 102 Adjacency Matrix 2 15 4 3 For an undirected graph, an adjacency matrix is symmetric across the main diagonal 25 Copyright © William C. Cheng AG =AG =V3V3 V1V2V1V2 V4V5V4V5 V 1 V 2 V 3 V 4 V 5 1010 1111 0101 0101 1010 01010 0101 1010 0101 0101 1111
18
Can be useful for dense graphs (where E is close to V ) less than V ) Space complexity: O(V ) The Good Provides quick lookup to tell if two vertices are connected by an edge Adjacency Matrix Really bad for very sparse graphs (where E much 26 Data Structures - CSCI 102 Copyright © William C. Cheng Graphs 2 The Bad Wastes a lot of memory 2222
19
G is a graph with N vertices Adjacency List 27 Data Structures - CSCI 102 Copyright © William C. Cheng Graphs Each vertex V has a linked list of pointers to all the other vertices to which it is connected Vertices stored in arbitrary order
20
2121 4545 V2V3V2V3 Data Structures - CSCI 102 Adjacency List Vertex List 3 V 1 V 4 V 5 28 Copyright © William C. Cheng
21
2121 4545 Data Structures - CSCI 102 Adjacency List Vertex List 3 V2V3V2V3 V1V1 V 4 V 5 29 Copyright © William C. Cheng 52
22
2 15 4 Data Structures - CSCI 102 Adjacency List Vertex List 3 V3V3 V2V2 V1V1 V 4 V 5 30 Copyright © William C. Cheng 5143 52
23
2121 4545 Data Structures - CSCI 102 Adjacency List Vertex List 3 V2V3V2V3 V1V1 V 4 V 5 31 Copyright © William C. Cheng 43 52 5252 1414
24
2121 4545 Data Structures - CSCI 102 Adjacency List Vertex List 3 V2V3V2V3 V1V1 V4V4 V 5 32 Copyright © William C. Cheng 43 52 5252 1414 325
25
33 Copyright © William C. Cheng 2121 4545 Data Structures - CSCI 102 Adjacency List Vertex List 3 V2V3V2V3 V1V1 V4V5V4V5 43 52 5252 1414 3131 2222 5454
26
34 Copyright © William C. Cheng 2121 4545 Data Structures - CSCI 102 Adjacency List Vertex List 3 V2V3V2V3 V1V1 V4V5V4V5 43 52 5252 1414 3131 2222 5454
27
35 Copyright © William C. Cheng 2121 4545 Data Structures - CSCI 102 Adjacency List Vertex List 3 V2V3V2V3 V1V1 V4V5V4V5 43 52 5252 1414 3131 2222 5454 For an undirected graph, very edge is represented twice
28
The Good More space efficient than adjacency matrix Adjacency List Space used is proportional to the number of edges 36 Data Structures - CSCI 102 Copyright © William C. Cheng Graphs Space Complexity: O(V + E) The Bad Can be slower to tell if two vertices are connected Must look through the entire adjacency list for a particular vertex
29
If you had to search for a path between two nodes in the graph, how would you do it? Now that we have created a graph structure, how do we go about navigating it? 37 Data Structures - CSCI 102 Copyright © William C. Cheng Graphs Traversal What are the issues? Graphs can have cycles You might not be able to visit all the nodes in a graph starting from a single vertex Note: you don’t know how many hops they are away from each other
30
Given a graph G and a source vertex S Breadth First Search (BFS) 38 Data Structures - CSCI 102 Copyright © William C. Cheng Graphs Traversal Systematically explore G to find all the nodes reachable from S Visit all of the neighbors of S, then visit all the neighbors of S’s neighbors, etc. Computes the distance from S to each reachable vertex V BFS finds the shortest path (in terms of the number of hops) to each vertex from S
31
Breadth First Search (BFS) 39 Data Structures - CSCI 102 Copyright © William C. Cheng Graphs Traversal Given a graph G and a source vertex S Basic Algorithm Look at all vertices 1 step away from S In general Look at all vertices K steps away from S Once all vertices 1 step away from S have been visited, visit all vertices 2 steps away from S (but not 1 step away from S) Once all vertices K steps away from S have been visited, visit all vertices K+1 steps away from S (but not less than or equal to K steps away from S)
32
Breadth First Search (BFS) 40 Data Structures - CSCI 102 Copyright © William C. Cheng Graphs Traversal S Ex: connected rooms in an adventure game
33
Breadth First Search (BFS) 41 Data Structures - CSCI 102 Copyright © William C. Cheng Graphs Traversal 1 S is the first node visited Label it 1 (label all other nodes 0 to mean that they are un-labeled)
34
14 3 2 Data Structures - CSCI 102 Graphs Traversal Breadth First Search (BFS) S is the first node visited Visit all S’s neighbors These nodes are 1 hop away from S Visit all of them, label them 2, 3, and 4 What does "visit" mean? It means do what you need to do (in this case, label it; anything else?) 42 Copyright © William C. Cheng
35
14 3 2 5 6 43 Copyright © William C. Cheng Data Structures - CSCI 102 Graphs Traversal Breadth First Search (BFS) S is the first node visited Visit all S’s neighbors Start with nodes that are 1 hop away from S (i.e., 2, 3, 4) Visit node with label 2 first Visit all its neighbors that have not been visited, label them 5 and 6
36
14 3 2 5 6 44 Copyright © William C. Cheng Data Structures - CSCI 102 Graphs Traversal Breadth First Search (BFS) S is the first node visited Visit all S’s neighbors Start with nodes that are 1 hop away from S (i.e., 2, 3, 4) Visit node with label 3 next All its neighbors are visited already
37
14 3 2 5 6 45 Copyright © William C. Cheng 8 7 Data Structures - CSCI 102 Graphs Traversal Breadth First Search (BFS) S is the first node visited Visit all S’s neighbors Start with nodes that are 1 hop away from S (i.e., 2, 3, 4) Visit node with label 4 next Visit all its neighbors that have not been visited, label them 7 and 8
38
Data Structures - CSCI 102 Graphs Traversal Breadth First Search (BFS) 14 3 2 5 6 8 7 9 10 S is the first node visited Visit all S’s neighbors Start with nodes that are 1 hop away from S (i.e., 2, 3, 4) label them 9 and 10 46 Copyright © William C. Cheng Continue with nodes that are 2 hop away from S (i.e., 5, 6, 7, 8) Visit node with label 5 first Visit all its neighbors that have not been visited,
39
Data Structures - CSCI 102 Graphs Traversal Breadth First Search (BFS) 14 3 2 5 6 8 7 9 10 S is the first node visited Visit all S’s neighbors Start with nodes that are 1 hop away from S (i.e., 2, 3, 4) have not been visited, label it 11 47 Copyright © William C. Cheng Continue with nodes that are 2 hop away from S (i.e., 5, 6, 7, 8) Visit node with label 6 next Visit all its neighbors that 11
40
Data Structures - CSCI 102 Graphs Traversal Breadth First Search (BFS) 14 3 2 6 8 7 9595 10 12 11 S is the first node visited Visit all S’s neighbors Start with nodes that are 1 hop away from S (i.e., 2, 3, 4) have not been visited, label it 12 48 Copyright © William C. Cheng Continue with nodes that are 2 hop away from S (i.e., 5, 6, 7, 8) Visit node with label 7 next Visit all its neighbors that
41
49 Copyright © William C. Cheng Data Structures - CSCI 102 Graphs Traversal Breadth First Search (BFS) 14 3 2 6 8 7 9595 10 12 11 S is the first node visited Visit all S’s neighbors Start with nodes that are 1 hop away from S (i.e., 2, 3, 4) already Continue with nodes that are 2 hop away from S (i.e., 5, 6, 7, 8) Visit node with label 8 next All its neighbors are visited
42
Data Structures - CSCI 102 Graphs Traversal Breadth First Search (BFS) 14 3 2 6 8 7 9595 10 12 11 S is the first node visited Visit all S’s neighbors Start with nodes that are 1 hop away from S (i.e., 2, 3, 4) All their neighbors are visited already 50 Copyright © William C. Cheng Continue with nodes that are 2 hop away from S (i.e., 5, 6, 7, 8) Continue with nodes that are 3 hop away from S (i.e., 9, 10, 11, 12)
43
1 4 3 2 6 8 9595 10 12 7 11 Data Structures - CSCI 102 Graphs Traversal Breadth First Search (BFS) How do you know that you are done? Quick way You can keep a count of the number of nodes that are labeled with a zero When that reaches zero, you are done What if your graph is not connected? Long way See if you have discovered any new nodes at a new distance 51 Copyright © William C. Cheng
44
52 Copyright © William C. Cheng 10 6 3 1 24 5 9 87 1211 14 3 2 5 6 8 7 9 10 12 11 Data Structures - CSCI 102 Graphs Traversal Breadth First Search (BFS) Found a tree, rooted at S, in the original graph
45
53 Copyright © William C. Cheng 10 6 3 1 24 5 9 87 1211 Level( ) = 0 Level( ) = 1 Level( ) = 2 Level( ) = 3 Data Structures - CSCI 102 Graphs Traversal Breadth First Search (BFS) Found a tree, rooted at S, in the original graph Since it’s a tree, node have levels (distance from root)
46
54 Copyright © William C. Cheng Data Structures - CSCI 102 Graphs Traversal Breadth First Search (BFS) Found a tree, rooted at S, in the original graph Since it’s a tree, node have levels (distance from root) Level corresponds to length of shortest path from S 10 6 3 1 24 5 9 87 1211 14 3 2 5 6 8 7 9 10 12 11
47
55 Data Structures - CSCI 102 Graphs Traversal Breadth First Search (BFS) Algorithm Queue-based search algorithm start at vertex S mark S as visited, level[S] = 0, parent[S] = NULL add S to search queue while (the search queue is not empty) { dequeue the first vertex, call this u for each vertex v adjacent to u { if (v has not been visited) { mark v as visited, level[v] = level[u] + 1 add v to the search queue parent[v] = u } Copyright © William C. Cheng
48
57 Copyright © William C. Cheng Data Structures - CSCI 102 Breadth First Search Search Queue S Start ∞ W R ∞ V S level=0 T∞∞XT∞∞X U∞∞YU∞∞Y
49
58 Copyright © William C. Cheng Data Structures - CSCI 102 Breadth First Search Search Queue T∞∞XT∞∞X U∞∞YU∞∞Y R 1 ∞ V W level=1 S 0 1 W R level=1
50
59 Copyright © William C. Cheng Data Structures - CSCI 102 Breadth First Search Search Queue R level=1 U∞∞YU∞∞Y T level=2 X level=2 R1∞VR1∞V S01WS01W T22XT22X
51
60 Copyright © William C. Cheng Data Structures - CSCI 102 Breadth First Search Search Queue U∞∞YU∞∞Y T level=2 X level=2 V level=2 S01WS01W T22XT22X R12VR12V
52
61 Copyright © William C. Cheng Data Structures - CSCI 102 Breadth First Search Search Queue X level=2 V level=2 U level=3 R12VR12V S01WS01W T22XT22X U3∞YU3∞Y
53
62 Copyright © William C. Cheng Data Structures - CSCI 102 Breadth First Search Search Queue V level=2 U level=3 Y level=3 R12VR12V S01WS01W T22XT22X U33YU33Y
54
63 Copyright © William C. Cheng Data Structures - CSCI 102 Breadth First Search Search Queue R 1 2 V U level=3 S 0 1 W Y level=3 T22XT22X U33YU33Y
55
64 Copyright © William C. Cheng Data Structures - CSCI 102 Breadth First Search Search Queue R 1 2 V Y level=3 S01WS01W T22XT22X U33YU33Y
56
Data Structures - CSCI 102 Breadth First Search Search Queue 65 Copyright © William C. Cheng R12VR12V S01WS01W T22XT22X U33YU33Y
57
Each vertex is only ever enqueued or dequeued once: O(V) total NOTE: The sum of the size of all the adjacency lists is V+E Breadth First Search (BFS) Complexity 66 Data Structures - CSCI 102 Copyright © William C. Cheng Graph Traversal Each vertex adjacency list is scanned only once (when vertex is dequeued): O(E) total Initialization overhead: O(V) Overall complexity: O(V + E)
58
Systematically explore G to find all the nodes reachable from S Given a graph G and a source vertex S Depth First Search (DFS) 68 Data Structures - CSCI 102 Copyright © William C. Cheng Graph Traversal Visit all of the neighbors of the most recently discovered vertex Backtrack once all options are exhausted Resembles preorder traversal of a binary tree
59
Given a graph G and a source vertex S Basic Algorithm Continue taking the first neighbor at each step and backtrack once you’re out of options Depth First Search (DFS) 69 Data Structures - CSCI 102 Copyright © William C. Cheng Graph Traversal In general Look at the first neighbor v of S Look at the first neighbor of v Once a DFS bottoms out, backtrack to the most recently found unvisited vertex and start DFS As soon as you visit a vertex, start doing a DFS traversal from that vertex
60
Depth First Search (DFS) 70 Data Structures - CSCI 102 Copyright © William C. Cheng Graphs Traversal Let’s use a tree as an example S
61
Depth First Search (DFS) 71 Data Structures - CSCI 102 Copyright © William C. Cheng Graphs Traversal Let’s use a tree as an example 1
62
Depth First Search (DFS) 72 Data Structures - CSCI 102 Copyright © William C. Cheng Graphs Traversal Let’s use a tree as an example 1 2
63
Depth First Search (DFS) 73 Data Structures - CSCI 102 Copyright © William C. Cheng Graphs Traversal Let’s use a tree as an example 1 2 3
64
Depth First Search (DFS) 74 Data Structures - CSCI 102 Copyright © William C. Cheng Graphs Traversal Let’s use a tree as an example 1 2 3 4
65
75 Copyright © William C. Cheng Data Structures - CSCI 102 Graphs Traversal Depth First Search (DFS) Let’s use a tree as an example 1 2 3 45
66
76 Copyright © William C. Cheng Data Structures - CSCI 102 Graphs Traversal Depth First Search (DFS) Let’s use a tree as an example 1 2 3 45 6
67
77 Copyright © William C. Cheng Data Structures - CSCI 102 Graphs Traversal Depth First Search (DFS) Let’s use a tree as an example 1 2 3 45 6 7
68
78 Copyright © William C. Cheng Data Structures - CSCI 102 Graphs Traversal Depth First Search (DFS) Let’s use a tree as an example 1 4 3535 2626 78
69
79 Copyright © William C. Cheng Data Structures - CSCI 102 Graphs Traversal Depth First Search (DFS) Let’s use a tree as an example 1 2 4 3535 6 78 9
70
80 Copyright © William C. Cheng Data Structures - CSCI 102 Graphs Traversal Depth First Search (DFS) Let’s use a tree as an example 1 2 3 45 6 78 9 10
71
81 Copyright © William C. Cheng Data Structures - CSCI 102 Graphs Traversal Depth First Search (DFS) Let’s use a tree as an example 1 2 3 45 6 78 9 1011
72
82 Copyright © William C. Cheng Data Structures - CSCI 102 Graphs Traversal Depth First Search (DFS) Let’s use a tree as an example 1 2 3 45 6 78 9 1011 12
73
Generally a recursive algorithm Depth First Search (DFS) Algorithm 83 Data Structures - CSCI 102 Copyright © William C. Cheng Graph Traversal Can also be Stack-based start at vertex S mark S as visited for each vertex v adjacent to S { if v has not been visited { start DFS from v }}}}
74
85 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search VYVY U Start X WZWZ
75
Data Structures - CSCI 102 Depth First Search VW XY U Start Z VWVW Adjacency List U X Y Z 86 Copyright © William C. Cheng
76
87 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search UVWUVW X VYyVYy XZXZ V UVW XY Start YZYZ XZXZ Z Adjacency List
77
88 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search UVWUVW X VYyVYy XZXZ V UVW XY YZYZ XZXZ Z Adjacency List 1/ Start DFS at Vertex U (Step 1)
78
89 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search W V U X Y VX yZ V UVW XY YZYZ XZXZ Z Adjacency List 1/ Visit Vertex V (Step 2) 2/
79
90 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search W V U X Y VX yZ V UVW XY YZYZ XZXZ Z Adjacency List 1/ Visit Vertex Y (Step 3) 2/ 3/
80
91 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search W V U X Y VX yZ V UVW XY YZYZ XZXZ Z Adjacency List 1/ Visit Vertex X (Step 4) 2/ 3/4/
81
92 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search W V U X Y VX yZ V UVW XY YZYZ XZXZ Z Adjacency List 1/2/ 3/4/5 Complete Vertex X (Step 5) NOTE: Vertex V already visited
82
93 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search W V U X Y VX yZ V UVW XY YZYZ XZXZ Z Adjacency List 1/ Complete Vertex Y (Step 6) 2/ 3/64/5
83
94 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search W V U X Y VX yZ V UVW XY YZYZ XZXZ Z Adjacency List 1/ Complete Vertex V (Step 7) 2/7 3/64/5
84
95 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search W V U X Y VX yZ V UVW XY YZYZ XZXZ Z Adjacency List 1/82/7 3/64/5 Complete Vertex U (Step 8) NOTE: Vertex X already visited
85
96 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search W V U X Y Y VX yZ V X UVW XY Z Z Z Adjacency List 1/82/7 3/64/5 another root (i.e., you can build a forest)? Search bottomed out Restart search from another Vertex But how do you know that there is
86
node (does not even have to be Data Structures - CSCI 102 Depth First Search What to do if search bottomed out? U V W X Y Z 2/7 3/6 1/8 4/5 Depends on the goal of our DFS Possible goals 1) Perform a DFS starting from a particular a node that has no incoming edge) 2) Perform a DFS of the whole graph, starting with all nodes that do not have incoming edges How do you find these nodes? 97 Copyright © William C. Cheng
87
98 Complexity: O(V + E) Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search Find all nodes that do not have incoming edges Maintain the following information: count[v] = number of incoming edges for node v S = set of nodes with no incoming edges for each vertex v { count[v] = 0 } for each vertex v { for each vertex u adjacent to v { count[u]++ } } S = empty set for each vertex v { if (count[v] == 0) { S = S {v} }
88
99 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search Find all nodes that do not have XZXZ UVWXYZUVWXYZ VYyVXZVYyVXZ Adjacency Listcount[] 000000000000 incoming edges Initialize all count[] to 0
89
100 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search Find all nodes that do not have V U Y VX Z WXYZWXYZ yVXZyVXZ Adjacency Listcount[] 1 0 01000100 incoming edges Initialize all count[] to 0 Process neighbors of U
90
101 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search Find all nodes that do not have V U Y VX Z WXYZWXYZ yVXZyVXZ Adjacency Listcount[] 1 0 01100110 incoming edges Initialize all count[] to 0 Process neighbors of U Process neighbors of V
91
102 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search Find all nodes that do not have V U Y VX Z WXYZWXYZ yVXZyVXZ Adjacency Listcount[] 1 0 01210121 incoming edges Initialize all count[] to 0 Process neighbors of U Process neighbors of V Process neighbors of W
92
103 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search Find all nodes that do not have V U Y VX Z WXYZWXYZ yVXZyVXZ Adjacency Listcount[] 2 0 01210121 incoming edges Initialize all count[] to 0 Process neighbors of U Process neighbors of V Process neighbors of W Process neighbors of X
93
104 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search Find all nodes that do not have V U Y VX Z WXYZWXYZ yVXZyVXZ Adjacency Listcount[] 2 0 02210221 incoming edges Initialize all count[] to 0 Process neighbors of U Process neighbors of V Process neighbors of W Process neighbors of X Process neighbors of Y
94
105 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search Find all nodes that do not have V U Y VX Z WXYZWXYZ yVXZyVXZ Adjacency Listcount[] 2 0 02210221 incoming edges Initialize all count[] to 0 Process neighbors of U Process neighbors of V Process neighbors of W Process neighbors of X Process neighbors of Y Process neighbors of Z Do not increment count[Z] because it’s a self-reference
95
106 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search Find all nodes that do not have V U Y VX Z WXYZWXYZ yVXZyVXZ Adjacency Listcount[] 2 0 02210221 incoming edges Initialize all count[] to 0 Process neighbors of U Process neighbors of V Process neighbors of W Process neighbors of X Process neighbors of Y Process neighbors of Z Done Add nodes U and W to S
96
107 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search X YZYZ W 0 count[] U 0 V 2 2 2121 VW XY U Start Z Correctly identifies that nodes U and W do not have a parent
97
Search bottomed out Restart search from another Vertex W V U X Y VX yZ V UVW XY YZYZ XZXZ Z Adjacency List 1/82/7 3/64/5 108 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search
98
W V U X Y VX yZ V UVW XY YZYZ XZXZ Z Adjacency List 1/82/7 3/64/5 109 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search Start DFS at Vertex W (Step 9) NOTE: Found a node whose "count" is 0 9/
99
V U WXWX Y VX ZyVyV UVW YZYZ XZXZ Adjacency List 1/82/7 3/6 Y 4/5 X 110 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search Visit Vertex Z (Step 10) NOTE: Vertex Y already visited 9/ 10 / Z
100
V U WXWX Y VX ZyVyV UVW YZYZ XZXZ Adjacency List 1/82/7 3/6 Y 4/5 X 111 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search Complete Vertex Z (Step 11) NOTE: Vertex Z already visited 9/ 10 / 11 Z
101
Complete Vertex W (Step 12) V U WXWX Y VX ZyVyV UVW YZYZ XZXZ Adjacency List 1/82/7 3/6 Y 4/5 X 112 Copyright © William C. Cheng Data Structures - CSCI 102 Depth First Search 9 / 12 10 / 11 Z
102
Initialization overhead: O(V) Based on marking nodes as visited, we can assume E executions total Depth First Search (DFS) Complexity 113 Data Structures - CSCI 102 Copyright © William C. Cheng Graph Traversal DFS is recursively called once for each vertex Overall complexity: O(V + E)
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.