Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.

Similar presentations


Presentation on theme: "Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent."— Presentation transcript:

1 Graphs & Paths Presentation : Part II

2 Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent for algorithms: – Adjacency lists. –Adjacency matrix. When expressing the running time of an algorithm, it’s often in terms of both |V| and |E|.

3 Adjacency lists Array Adj of |V| lists, one per vertex. Vertex u’s list has all vertices v such that (u, v) ∈ E. (Works for both directed and undirected graphs.)

4 Example: For an undirected graph:

5 Example For a directed graph: Same asymptotic space and time.

6 Adjacency matrix

7

8 Breadth-first search Input: Graph G = (V, E), either directed or undirected, and source vertex s ∈ V. Output: d[v] = distance (smallest # of edges) from s to v, for all v ∈ V.

9 Idea Idea: Send a wave out from s. –First hits all vertices 1 edge from s. –From there, hits all vertices 2 edges from s. –Etc. Use FIFO queue Q to maintain wavefront. v ∈ Q if and only if wave has hit v but has not come out of v yet.

10 Breadth-First Search

11  rstu xvwy 

12  0  rstu xvwy  s Q 0

13 1  01  rstu xvwy  r Q 1 w 1

14 1  201  rstu xvwy  w Q 1 v 2

15 1  2012 rstu xvwy 2  v Q 2 t 2 x 2

16 1  2012 rstu xvwy 2  t Q 2 x 2

17 132012 rstu xvwy 2  x Q 2 u 3

18 132012 rstu xvwy 23 u Q 3 y 3

19 132012 rstu xvwy 23 y Q 3

20 132012 rstu xvwy 23  Q

21 Run-time = O(V+E) Shortest-path tree –the final weight is the minimum distance –keep predecessors and get the shortest path –all BFS shortest paths make a tree

22 Breadth-First Search Example

23 Breadth-First Search Since each vertex gets a finite d value at most once, values assigned to vertices are monotonically increasing over time. Time = O(V + E). –O(V) because every vertex enqueued at most once. –O(E) because every vertex dequeued at most once and we examine (u, v) is in E only when u is dequeued. Therefore, every edge examined at most once if directed, at most twice if undirected.

24 Depth-first search Input: G = (V, E), directed or undirected. No source vertex given! Output: 2 timestamps on each vertex: d[v] = discovery time f [v] = finishing time

25 Depth-first search Will methodically explore every edge. –Start over from different vertices as necessary. As soon as we discover a vertex, explore from it. –Unlike BFS, which puts a vertex on a queue so that we explore from it later. As DFS progresses, every vertex has a color: –WHITE = undiscovered –GRAY = discovered, but not finished (not done exploring from it) –BLACK = finished (have found everything reachable from it) Discovery and finish times: Unique integers from 1 to 2|V|. For all v, d[v] < f [v]. In other words, 1 ≤ d[v] < f [v] ≤ 2|V|.

26

27 Depth-First Search Methodically explore all vertices and edges All vertices are White, t = 0 For each vertex u do if u is White then Visit(u) Procedure Visit(u) color u Gray; d[u]  t  t +1 for each v adjacent to u do if v is White then Visit(v) color u Black f [u]  t  t +1 Gray vertices = stack of recursive calls

28 Depth-First Search 36872415

29 36872145 1

30 3687 2 145 1 2

31 4 3687 2 15 1 2 3

32 4 3687 2 15 1 2 3

33 4 3687 2 15 1 2 3

34 4 3687215 1 2 3

35 4 3687215 1 2 3 4

36 4 3687215 1 2 3 4

37 4 3687215 1 2 3 4

38 4 3687215 1 2 3 4 5

39 4 3687215 1 2 3 4 5

40 4 3687215 1 2 3 4 5 6

41 4 3687215 1 2 3 4 5 6

42 4 3687215 1 2 3 4 5 6

43 4 3687215 1 2 3 4 5 6

44 4 3687215 1 2 3 4 5 6

45 4 3687215 1 2 3 4 5 6 7

46 4 3687215 1 2 3 4 5 6 7

47 4 3687215 1 2 3 4 5 6 7 8

48 4 3687215 1 2 3 4 5 6 7 8

49 4 3687215 1 2 3 4 5 6 7 8

50 4 3687215 1 2 3 4 5 6 7 8

51 4 3687215 1 2 3 4 5 6 7 8

52 Review: Depth-First Search Depth-first search is another strategy for exploring a graph –Explore “deeper” in the graph whenever possible. –Edges are explored out of the most recently discovered vertex v that still has unexplored edges. –When all of v’s edges have been explored, backtrack to the vertex from which v was discovered

53 DFS Applications. Testing whether graph is connected. Computing a spanning forest of graph. Computing a path between two vertices of graph or equivalently reporting that no such path exists. Computing a cycle in graph or equivalently reporting that no such cycle exists.

54 DFS vs. BFS DFS seems cleaner than BFS: recursive, it doesn't make use of an external data structure (a queue), and is shorter too. But it is important to realize these two algorithms address very different needs. An example of BFS usage is calculating shortest paths. A Depth First Search performed on the World Wide Web would quickly overload any given web server with an ever growing number of requests. Therefore a Breadth First Search is preferred, since it dispatches requests to many servers at a time.

55 DFS vs. BFS ApplicationsDFSBFS Spanning forest, connected components, paths, cycles  Shortest paths  Biconnected components 

56 Example 2

57 Example 2 animated source vertex

58 DFS Example 1 | | | | | | | | source vertex d f

59 DFS Example 1 | | | | | | 2 | | source vertex d f

60 DFS Example 1 | | | | |3 | 2 | | source vertex d f

61 DFS Example 1 | | | | |3 | 4 2 | | source vertex d f

62 DFS Example 1 | | | |5 |3 | 4 2 | | source vertex d f

63 DFS Example 1 | | | |5 | 63 | 4 2 | | source vertex d f

64 DFS Example 1 |8 | | |5 | 63 | 4 2 | 7 | source vertex d f

65 DFS Example 1 |8 | | |5 | 63 | 4 2 | 7 | source vertex d f

66 DFS Example 1 |8 | | |5 | 63 | 4 2 | 79 | source vertex d f What is the structure of the yellow vertices? What do they represent?

67 DFS Example 1 |8 | | |5 | 63 | 4 2 | 79 |10 source vertex d f

68 DFS Example 1 |8 |11 | |5 | 63 | 4 2 | 79 |10 source vertex d f

69 DFS Example 1 |128 |11 | |5 | 63 | 4 2 | 79 |10 source vertex d f

70 DFS Example 1 |128 |1113| |5 | 63 | 4 2 | 79 |10 source vertex d f

71 DFS Example 1 |128 |1113| 14|5 | 63 | 4 2 | 79 |10 source vertex d f

72 DFS Example 1 |128 |1113| 14|155 | 63 | 4 2 | 79 |10 source vertex d f

73 DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f

74 Snapshot


Download ppt "Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent."

Similar presentations


Ads by Google