16-Graphs - Graphs G =(V E and we use v and e to denote...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Graphs G = (V, E) and we use v and e to denote, respectively, the # vertices in V and edges in E. Breadth-first-search This is analogous to a level traversal of a binary tree. Each vertex x has a color, color[x], a distance d[x], and a parent, Л[x] Q is a FIFO queue BFS(G, s): for each u ε V, u ≠ s color[u]=WHITE, d[u]=∞, Л[u]=null color[s]=GRAY, d[s]=0, Л[s]=null set Q to empty, enqueue(Q, s) while (Q is not empty) u=dequeue(Q) for each vertex v adjacent to u if (color[v] is WHITE) set color[v] to GRAY, d[v] to d[u]+1, and Л[v]=u enqueue (Q, v) set color[u] to BLACK Think of WHITE - as not seen, GRAY - as seen, BLACK - as done (all children seen). Give the example graph and discuss it! BFS(G, s) creates a spanning tree determined by Л . The root is s and d[v] is the distance from s to v. If all edges have weight 1 then d[u] is a minimum distance path from s to u. Execution time: The initialization takes O(v) time since we assume queue operations take O1) time. In the while loop each vertex is encountered once when it is WHITE and at that time it is made GRAY and put in the queue. It is taken out only one time and never put in again since it remains GRAY until taken out and it then becomes BLACK. So the total time relating to queue operations is O(v). Each adjacency list is traversed just once so the total traversal time for them all is O(e).Thus the total time is O(v+e). All of the above for BFS(G, s) applies to both directed and undirected graphs. Related Application - Dijkstra’s single-source shortest paths algorithm: Assume we are given a directed weighted graph G with non-negative weights w(u, v) for u, v ε V with w(u, v)=∞ if (u, v) is not in E. We want to find a shortest path from vertex s to each vertex of V where the “distance” of a path is the sum of the weights of its edges. Thus this a generalization of the minimum distance problem solved by BFS(G, s). The algorithm uses a set S that contains those vertices whose minimum paths and minimum distances from s are known. In BFS(G, s) the nodes colored BLACK would be in S.
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Dijkstra (G, w, s): for each u ε V, u ≠ s d[u]=∞, Л[u]=null d[s]=0, Л[s]=null set S to empty and make Q a priority queue containing all vertices of Von the basis of their d values. while (Q is not empty) u=extract-min (Q) and add u to S. for each vertex v adjacent to u and not in S if ( d[v] > d[u] + w(u, v) ) set d[v] to d[u]+w(u, v) and Л[v]=u. Give the example and discuss it!
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern