Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.

Similar presentations


Presentation on theme: "Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map."— Presentation transcript:

1 Graphs II Kruse and Ryba Chapter 12

2 Undirected Graph Example: Subway Map

3 Shortest Path Problem What is the length of the shortest path between s and any other vertex in the graph? For now, assume that length of path is defined as the number of edges in the path. In the case of the subway example, this would allow us to compute the answer to the question: What is the length of the shortest path between a particular station s and any other subway station?

4 Breadth-first Search Given: –Graph G=(V, E) in edge list representation –Selected source vertex, s Systematically explore the edges of G to discover every vertex that is reachable from s.

5 Breadth-first Search Breadth-first search is so named because it expands the frontier between discovered and undiscovered vertices uniformly across the breadth of the frontier. To keep track of progress, breadth-first search colors each vertex white, gray or black: –All vertices start out white (undiscovered), –The first time a vertex is discovered, its color is set to gray, –When processing of the vertex is complete, its color is set to black.

6 Breadth-first Search Algorithm Variables used in breadth first search:  [v]: A vertex v is discovered by traversing the adjacency list of an already discovered vertex v. We say that u is a predecessor of v:  [v] = u. d[v]: The length of the shortest path from s to v. color[v]: The color of the vertex v. Q: a FIFO queue used to manage gray vertices.

7 Breadth-First Search: Initialization Given graph G=(V,E), source vertex s  V Create empty queue Q For each vertex u  V – {s} { color[u]  white d[u]    [u]  NIL } Color[s]  gray D[s]  0  [s]  NIL Q  {s} 0 3 2 1 5 4 Q =  s

8 Breadth-First Search: Initialization Given graph G=(V,E), source vertex s  V Create empty queue Q For each vertex u  V – {s} { color[u]  white d[u]    [u]  NIL } Color[s]  gray D[s]  0  [s]  NIL Q  {s} 0 3 2 1 5 4 Q =  s d[u] = [ , , , , ,  ]  [u] = [ NIL, NIL, NIL, NIL, NIL, NIL ]

9 Breadth-First Search: Initialization Given graph G=(V,E), source vertex s  V Create empty queue Q For each vertex u  V – {s} { color[u]  white d[u]    [u]  NIL } Color[s]  gray D[s]  0  [s]  NIL Q  {s} 0 3 2 1 5 4 Q =  s 0 d[u] = [0, , , , ,  ]  [u] = [ NIL, NIL, NIL, NIL, NIL, NIL ]

10 Breadth-First Search: Main Loop While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray d[v]  d[u] + 1  [v]  u Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 Q =  u 0 d[u] = [0, , , , ,  ]  [u] = [ NIL, NIL, NIL, NIL, NIL, NIL ]

11 Breadth-First Search: Main Loop While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray d[v]  d[u] + 1  [v]  u Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 Q =  123 0 u d[u] = [0, 1, 1, 1, ,  ]  [u] = [ NIL, 0, 0, 0, NIL, NIL ]

12 Breadth-First Search: Main Loop While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray d[v]  d[u] + 1  [v]  u Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 Q =  u 123 d[u] = [0, 1, 1, 1, ,  ]  [u] = [ NIL, 0, 0, 0, NIL, NIL ]

13 Breadth-First Search: Main Loop While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray d[v]  d[u] + 1  [v]  u Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 Q =  u 23 1 d[u] = [0, 1, 1, 1, ,  ]  [u] = [ NIL, 0, 0, 0, NIL, NIL ]

14 Breadth-First Search: Main Loop While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray d[v]  d[u] + 1  [v]  u Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 Q =  u 23 1 4 d[u] = [0, 1, 1, 1, 2,  ]  [u] = [ NIL, 0, 0, 0, 1, NIL ]

15 Breadth-First Search: Main Loop While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray d[v]  d[u] + 1  [v]  u Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 Q =  u 234 d[u] = [0, 1, 1, 1, 2,  ]  [u] = [ NIL, 0, 0, 0, 1, NIL ]

16 Breadth-First Search: Main Loop While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray d[v]  d[u] + 1  [v]  u Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 Q =  u 34 2 d[u] = [0, 1, 1, 1, 2,  ]  [u] = [ NIL, 0, 0, 0, 1, NIL ]

17 Breadth-First Search: Main Loop While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray d[v]  d[u] + 1  [v]  u Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 Q =  u 34 2 5 d[u] = [0, 1, 1, 1, 2, 2]  [u] = [ NIL, 0, 0, 0, 1, 2]

18 Breadth-First Search: Main Loop While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray d[v]  d[u] + 1  [v]  u Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 Q =  u 345 d[u] = [0, 1, 1, 1, 2, 2]  [u] = [ NIL, 0, 0, 0, 1, 2]

19 Breadth-First Search: Main Loop While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray d[v]  d[u] + 1  [v]  u Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 Q =  u 45 3 d[u] = [0, 1, 1, 1, 2, 2]  [u] = [ NIL, 0, 0, 0, 1, 2]

20 Breadth-First Search: Main Loop While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray d[v]  d[u] + 1  [v]  u Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 Q =  u 45 3 d[u] = [0, 1, 1, 1, 2, 2]  [u] = [ NIL, 0, 0, 0, 1, 2]

21 Breadth-First Search: Main Loop While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray d[v]  d[u] + 1  [v]  u Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 Q =  u 45 d[u] = [0, 1, 1, 1, 2, 2]  [u] = [ NIL, 0, 0, 0, 1, 2]

22 Breadth-First Search: Main Loop While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray d[v]  d[u] + 1  [v]  u Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 Q =  u 5 4 d[u] = [0, 1, 1, 1, 2, 2]  [u] = [ NIL, 0, 0, 0, 1, 2]

23 Breadth-First Search: Main Loop While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray d[v]  d[u] + 1  [v]  u Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 Q =  u 5 4 d[u] = [0, 1, 1, 1, 2, 2]  [u] = [ NIL, 0, 0, 0, 1, 2]

24 Breadth-First Search: Main Loop While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray d[v]  d[u] + 1  [v]  u Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 Q =  u 5 d[u] = [0, 1, 1, 1, 2, 2]  [u] = [ NIL, 0, 0, 0, 1, 2]

25 Breadth-First Search: Main Loop While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray d[v]  d[u] + 1  [v]  u Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 Q =  u 5 d[u] = [0, 1, 1, 1, 2, 2]  [u] = [ NIL, 0, 0, 0, 1, 2]

26 Breadth-First Search: Main Loop While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray d[v]  d[u] + 1  [v]  u Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 Q =  u 5 d[u] = [0, 1, 1, 1, 2, 2]  [u] = [ NIL, 0, 0, 0, 1, 2]

27 Breadth-First Search: Main Loop While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray d[v]  d[u] + 1  [v]  u Enqueue(Q,v) } Dequeue(Q) color[u]  black; } 0 3 2 1 5 4 Q =  u d[u] = [0, 1, 1, 1, 2, 2]  [u] = [ NIL, 0, 0, 0, 1, 2]

28 Breadth-First Search: Result 0 3 2 1 5 4 s d[u] = [0, 1, 1, 1, 2, 2]  [u] = [ NIL, 0, 0, 0, 1, 2] Given d[u] and  [u]… Q: What is the length of the shortest path from s to u? A: Retrieve value d[u]. Q: What is the shortest path from s to u? A: Follow predecessors stored in  [u].

29 Breadth-First Search: Result 0 3 2 1 5 4 s d[u] = [0, 1, 1, 1, 2, 2]  [u] = [ NIL, 0, 0, 0, 1, 2] Given d[u] and  [u]… Q: What is the length of the shortest path from s to 5? A: Retrieve value d[u]. Q: What is the shortest path from s to 5? A: Follow predecessors stored in  [u]:  [5] = 2,  [2] = 0,  [2] = NIL path = {0,2,5}

30 Breadth-first Strategy: Summary Breadth-first strategy is used in many graph algorithms (not just shortest path). Systematically explore the edges of G to discover every vertex that is reachable from s. Basic approach: –Start from source vertex s –Color vertices as they are discovered and finished –Maintain a queue of discovered vertices –Explore graph by following edges from vertices already in queue to discover new vertices

31 Graph Search Choice of container used to store discovered vertices… –If a queue is used as the container, we get breadth first search. –If a stack is used as the container, we get depth first search.

32 Professor Bumstead Gets Dressed shirt tie jacket socks shoes watch undershorts pants belt

33 Professor Bumstead Gets Dressed shirt tie jacket socks shoes watch undershorts pants belt Assume that Bumstead can only do one thing at time. Produce a list of tasks in order such that no task has an edge connecting it to a task earlier in the list.

34 Topological Sort Assume a directed acyclic graph G = (V,E). A topological sort of G is a linear ordering of all its vertices such that if G contains an edge (u,v), then u appears before v in the ordering. If the graph is cyclic, then no ordering is possible.

35 Topological Sort Example shirt tie jacket socks shoes watch undershorts pants belt socks undershorts pants shoes watch shirt belt tie jacket

36 Topological Sort Example shirt tie jacketsocks shoes watch undershortspantsbelt shirt tie jacket socks shoes watch undershorts pants belt

37 Depth-First Search: Initialization Given graph G=(V,E) Create empty stack S For each vertex u  V { color[u]  white d[u]    [u]  NIL } 0 3 2 1 5 4 S = 

38 Depth-First Search: Initialization Given graph G=(V,E) Create empty stack S For each vertex u  V { color[u]  white d[u]    [u]  NIL } 0 3 2 1 5 4 S = 

39 Depth-First Search: Main Loop for s  V if color[s] = white { Push(S, s); color[s]  gray while not Empty(S) { u  Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray d[v]  d[u] + 1  [v]  u Push(S,v) } color[u]  black; }

40 Recursive Depth First Search Given graph G=(V,E) for each vertex u  V[G] { color[u]  white  [u]  NIL } time  0 for each vertex u  V[G] if color[u] = white DFS-visit(u) DFS(G) color[u]  gray d[u]  time  time + 1 for each vertex v  Adjacent[u] if color[v] = white {  [v]  u DFS-visit(v) } color[u]  black f[u]  time  time + 1 DFS-visit(u)

41 Graph Search Choice of container used to store discovered vertices… –If a queue is used as the container, we get breadth first search. –If a stack is used as the container, we get depth first search.


Download ppt "Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map."

Similar presentations


Ads by Google