Download presentation

Presentation is loading. Please wait.

Published byShona Bradford Modified over 2 years ago

1
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway map, find a path from one city to another. What if the graph has cycles? –To avoid getting “trapped” in a cycle, mark the vertices that have already been visited.

2
2 Graphs: Breadth-First Traversal Input: Graph G=(V,E), source vertex s Idea: starting at the source, explore all vertices at distance i from the source before exploring those at distance i+1 Output: A tree with root s, containing all the vertices that can be reached from s and showing the path to follow to reach each vertex.

3
3 Graphs: Breadth-First Traversal The breadth first tree actually shows a shortest path between the source and each vertex (there may be more than one shortest paths, but it only shows one)

4
4 Graphs: Breadth-First Traversal Implementation details Use a queue to store the vertices that are to be examined next. Whenever a vertex is dequeued, its non- marked neighbors (i.e. neighbors that have not been placed in the queue yet) are enqueued. The FIFO structure ensures that all vertices at distance i are visited before any vertices at distance i+1.

5
5 Graphs: Breadth-First Traversal The algorithm Initialize the attributes Enqueue(s) As long as the queue is not empty Let v be the head of the queue For each unmarked neighbor u of v mark u Distance(u)=Distance(v)+1 Parent(u)=v Enqueue(u) Dequeue

6
6 Graphs: Breadth-First Traversal Time analysis Each vertex is placed into the queue exactly once => we have a total of O(|V|) queue operations Every time we add the neighbors of a vertex into the queue, we need to: –examine the corresponding row if using adjacency matrix In this case, since examining a row takes O(|V|) time and we do this once for each vertex, the total time for the examination is O(|V| 2 ) Then, the overall time for BFS is O(|V|+|V| 2 )=O(|V| 2 ) –or,

7
7 Graphs: Breadth-First Traversal Time analysis continued –examine the list of neighbors if using adjacency list What is the total size of the adjacency lists? Since each edge contributes two vertices in the lists, the total size of vertices in the lists (and consequently, the total time for the examination) is 2|E|. Then the overall time is O(|V|+|E|)

8
8 Graphs: Depth-First Traversal Input: Graph G=(V,E) Idea: starting at the source, explore vertices as far from the source as you can go. If you get stuck, backtrack to the most recent vertex that still has unexamined neighbors. Output: a tree with root s containing all the vertices that can reached from s and showing the path to follow in order to reach each node from s. We can also use this method to timestamp the vertices (more on that later)

9
9 Graphs: Depth-First Traversal Implementation details: To ensure that we travel as deep as possible in the graph before backtracking, use a stack to store the vertices that are to be examined. –In other words, this is a recursive procedure.

10
10 Graphs: Depth-First Traversal DF( ) for each v in V marked(v) = 0 parent(v) = NULL time = 0 for each v in V if marked(v) = = 0 DFVisit(v) marked(v) = 1 time = time + 1 start(v) = time for each neighbor u of v if marked(u) = = 0 parent(u) = v DFVisit(u) time = time + 1 finish(v) = time

11
11 Graphs: Depth-First Traversal Time Analysis: The initializing loop in DF() takes O(|V|) time DFVisit() takes a total of O(|E|) because it accesses the adjacency list of each vertex exactly once Total time: O(|V|+|E|)

12
12 Graphs: Topological Sort Topological sort –A topological sort of a directed acyclic graph 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. –Example : Given the prerequisites graph for the CS department, find a topological sort. –Solution : Use DF to get the “finish” timestamps Return the vertices in reverse “finish” order

13
13 Single-Source Shortest Path Main idea: estimate the shortest distance and keep updating the value as you traverse the graph. –In detail: Given an edge (u,v) where u and v have estimated distances d[u] and d[v] from a source, and the weight of (u,v) is w, check whether d[v] > d[u] + w. If so, then there's a shorter path from the source to v, and that's through u. Update the estimated distance for v and keep going. –This process is called relaxation (of an edge)

14
14 Dijkstra's algorithm Initialize d[source] to 0, all others to INFINITY Initialize a priority queue PQ to contain V while PQ is not empty v = PQ.Extract_Min() for each neighbor u of v if (d[u] > d[v] + w(v,u)) d[u] = d[v] + w(v,u) parent(u) = v

15
15 Dijkstra's algorithm Works on weighted, directed graphs with non-negative weights. Running time depends on priority queue implementation: –heap : O( (|V|+|E|) * log|V|)

Similar presentations

OK

More Graph Algorithms Weiss ch. 14. 2 Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.

More Graph Algorithms Weiss ch. 14. 2 Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on money and credit download Ppt on high voltage engineering holland Ppt on different layers of atmosphere Download ppt on coastal plains of india Ppt on solar energy collectors Ppt on different model of atom Ppt on venture capital for mba students Ppt on grease lubrication tubing Download ppt on design and construction of floating structure Ppt on hdfc mutual fund