1 / 15

Graphs: Traversal

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?

esterling
Download Presentation

Graphs: Traversal

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. 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. 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. 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. 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. 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. 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 isO(|V|+|E|)

  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. 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. Graphs: Depth-First Traversal 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 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)

  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. 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. 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. 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. 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|)

More Related