Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Similar presentations


Presentation on theme: "Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected."— Presentation transcript:

1 Graphs I Kruse and Ryba Chapter 12

2 Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected because its edges do not have a particular direction.

3 Edges We sometimes need to refer to the edges e i of a graph: e0e0 e1e1 e3e3 e4e4 e5e5 e2e2

4 Vertices We also need to refer to the vertices v j of a graph: v1v1 e0e0 e1e1 e3e3 e4e4 e5e5 e2e2 v2v2 v3v3 v4v4 v0v0

5 Undirected Graph Definition An undirected graph G = (V, E) is a finite set of vertices V, together with a finite set of edges E. Both sets might be empty (no vertices and no edges), which is called the empty graph. Each edge has associated with it two vertices. If two vertices are connected by an edge, they are said to be adjacent.

6 Undirected Graph Example: Subway Map

7 Another Undirected Graph: vBNS (fast internet backbone)

8 Directed Graphs In a directed graph each vertex has an associated direction; e.g.: Each edge is associated with two vertices: its source vertex and target vertex.

9 Directed Graph Examples Flow graph: vertices are pumping stations and edges are pipelines connecting them. Task network: each project has a number of component activities called tasks. Each task has a duration (amount of time needed to complete task).

10 Loops A loop is an edge that connects a vertex with itself; for example: Loops can occur in directed graphs or undirected graphs.

11 Multiple Edges In principle, a graph may have two or more edges connecting the same two vertices in the same direction: These are called multiple edges. In a graph diagram, each edge is drawn separately.

12 Simple Graphs A graph is said to be a simple graph, if: 1.the graph has no loops, and 2.no multiple edges

13 Path A path in a graph is a sequence of vertices, v 0, v 1, … v m, such that each adjacent pair of vertices in the list are connected by an edge. v1v1 v2v2 v3v3 v4v4 v0v0

14 Path A path in a graph is a sequence of vertices, v 0, v 1, … v m, such that each adjacent pair of vertices in the list are connected by an edge. v1v1 v2v2 v3v3 v4v4 v0v0 Example path: v 0, v 3, v 4

15 Path A path in a graph is a sequence of vertices, v 0, v 1, … v m, such that each adjacent pair of vertices in the list are connected by an edge. v1v1 v2v2 v3v3 v4v4 v0v0 Example path: v 0, v 3, v 4 v 0, v 3, v 2, v 1, v 4

16 Path In a directed graph, the connection must go from source v i to target v i+1. v1v1 v2v2 v3v3 v4v4 v0v0 Is there a path from v 0 to v 4 ? v 3 to v 1 ? v 1 to v 2 ? v 2 to v 1 ?

17 Example Task Network Start Find a realtor Look at homes Choose a home to buy Apply for financing and establish price limit Investigate neighborhood schools Prepare offer Investigate termite and soil reports Present offer 0 20 59 2 15 10 3 2 How long will it take to buy a house? (The duration of each task is shown below each task).

18 Critical Path Start Find a realtor Look at homes Choose a home to buy Apply for financing and establish price limit Investigate neighborhood schools Prepare offer Investigate termite and soil reports Present offer 0 20 59 2 15 10 3 2 The task will take as long as the maximum duration path from start to finish.

19 Cycles A cycle in a graph is a path that leads back to the first node in the path. v1v1 v2v2 v3v3 v4v4 v0v0 Example cycles: v 1, v 2, v 3, v 2, v 1

20 Cycles A cycle in a graph is a path that leads back to the first node in the path. v1v1 v2v2 v3v3 v4v4 v0v0 Example cycles: v 1, v 2, v 3, v 2, v 1 v 0, v 3, v 4, v 1, v 0

21 Does this graph have any cycles?

22 Cycles in Directed Graphs A cycle in a directed graph must follow edge directions; e.g.:

23 DAG: Directed Acyclic Graph A directed acyclic graph (DAG) must have no cycles; e.g.: In other words, there is no path that starts and ends at the same node.

24 Complete Graph An undirected graph which has an edge between every pair of vertices.

25 Connected Graph An undirected graph which has a path between every pair of vertices. 1 0 23

26 Strongly Connected Graph A directed graph which has a path between every pair of vertices.

27 Weakly Connected Graph A directed graph which is connected, but not not strongly connected.

28 A DAG that is not Connected

29 Connected Components

30 Review of Terms Undirected vs. directed graphs Multiple edges, loops Simple graph Path Cycle Directed, acyclic graph (DAG) Connected, strongly connected, and weakly connected graphs Connected component

31 Graph Implementations

32 Adjacency Matrix 0 1 3 2 0 1 2 3 01230123

33 Adjacency Matrix 0 1 3 2 1 0 1 2 3 01230123

34 Adjacency Matrix 0 1 3 2 1 0 0 1 2 3 01230123

35 Adjacency Matrix 0 1 3 2 1 0 1 0 1 2 3 01230123

36 Adjacency Matrix 0 1 3 2 1 0 1 1 0 1 2 3 01230123

37 Adjacency Matrix 0 1 3 2 1 0 1 1 0 0 0 1 0 1 2 3 01230123

38 Adjacency Matrix 0 1 3 2 1 0 1 1 0 0 0 1 1 0 0 0 0 1 2 3 01230123

39 Adjacency Matrix 0 1 3 2 1 0 1 1 0 0 0 1 1 0 0 0 0 1 0 0 0 1 2 3 01230123

40 C++ Adjacency Matrix 0 1 3 2 1 0 1 1 0 0 0 1 1 0 0 0 0 1 0 0 0 1 2 3 01230123 bool graph_adjacency[4][4];

41 Adjacency Matrix: Weighted Edges 0 1 3 2 5.8 06.01.5 0 0 02.5 0 0 0 04.2 0 0 0 1 2 3 01230123 double graph_adjacency[4][4]; 6.0 2.5 4.2 2.5 5.8 1.5

42 Weighted Edges: An Example $320 $245 San Fransisco Boston Chicago $452 $60 $180 Dallas Flights. What’s the cheapest way to fly San Fransisco  Boston?

43 Weighted Edges: An Example 1800 1200 San Fransisco Boston Chicago 2800 2300 1600 Dallas Mileage for various hops from San Fransisco  Boston* *Mileage is not real, but for sake of example.

44 Weighted Edges: An Example Fran Pat ADSL provider 100Kbs Available internet bandwidth on various paths between an ADSL provider and customers*. *This is a simplication for sake of example, and bit rates are fictional. 1Gbs 100Kbs 500Kbs

45 Adjacency Matrix: Undirected Graph 0 1 3 2 0 1 2 3 01230123 bool graph_adjacency[4][4];

46 Adjacency Matrix: Undirected Graph 0 1 3 2 0 0 0 0 0 1 2 3 01230123 No loops  zero diagonal.

47 Adjacency Matrix: Undirected Graph 0 1 3 2 0 1 1 1 0 0 0 0 1 2 3 01230123

48 Adjacency Matrix: Undirected Graph 0 1 3 2 0 1 1 1 1 0 0 1 0 0 0 1 2 3 01230123

49 Adjacency Matrix: Undirected Graph 0 1 3 2 0 1 1 1 1 0 0 1 1 0 0 0 0 0 1 2 3 01230123

50 Adjacency Matrix: Undirected Graph 0 1 3 2 0 1 1 1 1 0 0 1 1 0 0 0 1 1 0 0 0 1 2 3 01230123

51 Symmetry of Adjacency Matrix of Undirected Graph 0 1 3 2 0 1 1 1 1 0 0 1 1 0 0 0 1 1 0 0 0 1 2 3 01230123 a[i][j]== a[j][i]

52 Non-symmetric Adjacency Matrix for Directed Graph 0 1 3 2 1 0 1 1 0 0 0 1 1 0 0 0 0 1 0 0 0 1 2 3 01230123 a[i][j]  a[j][i]

53 Symmetric Adjacency Matrix An undirected graph will always have a symmetric adjacency matrix. A directed graph may not have a symmetric adjacency matrix. If a directed graph has a symmetric matrix, what does that mean?

54 Non-symmetric Adjacency Matrix Unweighted Directed Graph 0 1 3 2 1 0 1 1 0 0 0 1 1 0 0 0 0 1 0 0 0 1 2 3 01230123 a[i][j]  a[j][i]

55 Add an Edge 0 1 3 2 1 0 1 1 0 0 0 1 1 0 0 0 1 1 0 0 0 1 2 3 01230123 a[i][j]=a[j][i]

56 Adjacency Matrix for Weighted Directed Graph 0 1 3 2 5 0 3 6 0 0 0 4 3 0 0 0 6 4 0 0 0 1 2 3 01230123 a[i][j]=a[j][i] 3 3 2 6 6 4 4 5

57 Time Complexity Analysis Given N we have vertices in a graph, and we use the adjacency matrix representation. 1.What is the worst case and average complexity of inserting an edge in the graph? 2.What is the worst case and average complexity of removing an edge in the graph? 3.What is the complexity of retrieving a list of all nodes that share and edge with a particular vertex?

58 Representing Graphs with Edge Lists 0 1 3 2 2 3 Null Edge list for vertex 0 Null Edge list for vertex 1 0 Edge list for vertex 2 1 Null Edge list for vertex 3 Null

59 Time Complexity Analysis Given N we have vertices in a graph, and we use the edge list representation. 1.What is the worst case and average complexity of inserting an edge in the graph? 2.What is the worst case and average complexity of removing an edge in the graph? 3.What is the complexity of retrieving a list of all nodes that share and edge with a particular vertex?

60 Representing Graphs with Edge Sets 0 1 3 2 Assume a Set template class is available: Declare array of sets, one per node in graph: Set connections[N]; Each node’s set contains the indices of all vertices that it is connected to.

61 Time Complexity Analysis Given N we have vertices in a graph, and we use the edge set representation. 1.What is the worst case and average complexity of inserting an edge in the graph? 2.What is the worst case and average complexity of removing an edge in the graph? 3.What is the complexity of retrieving a list of all nodes that share and edge with a particular vertex?

62 Graph Implementations Summary Adjacency matrix Edge lists Edge sets

63 Graph Searching

64 Graph Search Choice of container –If a stack is used as the container for adjacent vertices, we get depth first search. –If a list is used as the container adjacent vertices, we get breadth first search.

65 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4

66 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 s Q = 

67 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 s Q = 

68 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 s Q = 0

69 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 u Q = 0

70 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 u Q = 01

71 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 u Q = 012

72 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 u Q = 0123

73 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 u Q = 0123

74 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 u Q = 0123

75 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 u Q = 01234

76 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 u Q = 01234

77 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 u Q = 01234

78 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 u Q = 01234

79 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 u Q = 01234

80 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 u Q = 01234

81 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 u Q = 012345

82 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 u Q = 012345

83 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 u Q = 012345

84 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 u Q = 012345

85 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 u Q = 012345

86 Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 u Q = 

87 Graph Search Choice of container –If a stack is used as the container for adjacent vertices, we get depth first search. –If a list is used as the container adjacent vertices, we get breadth first search.

88 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 s S = 

89 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 s S = 

90 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 s S =  0

91 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 u S = 

92 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 u S =  1

93 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 u S =  321

94 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 u S =  321

95 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 u S =  321

96 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 u S =  321

97 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 u S =  3215

98 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 u S =  3215

99 3 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 u S =  521

100 3 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 u S =  5214

101 3 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 u S =  5214

102 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 u S =  43521

103 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 u S =  43521

104 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 u S =  43521

105 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 u S =  43521

106 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 u S = 

107 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 u S = 

108 Graph Search Choice of container –If a stack is used as the container for adjacent vertices, we get depth first search. –If a list is used as the container adjacent vertices, we get breadth first search.

109 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } 0 3 2 1 5 4 u S = 


Download ppt "Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected."

Similar presentations


Ads by Google