1 / 21

Graph Traversals

Graph Traversals. A graph-traversal algorithm Visits all the vertices that it can reach starting at some vertex Visits all vertices of the graph iff the graph is connected (effectively computing Connected Components ) Must not loop forever, if a graph contains a cycle

Download Presentation

Graph Traversals

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. Graph Traversals • A graph-traversal algorithm • Visits all the vertices that it can reach starting at some vertex • Visits all vertices of the graph iff the graph is connected(effectively computing Connected Components) • Must not loop forever, if a graph contains a cycle • Must never visit a vertex more than once • connected component (for undirected graphs) = • The subset of vertices visited during a traversal that begins at a given vertex • strongly connected component (for directed graphs) = • The subset of vertices visited during a traversal that begins at any of its members

  2. A Search Problem • High Planes Airline Company (HPAir) Problem • For each customer request, indicate whether a sequence of HPAir flights exists from the origin city to the destination city • The flight map for HPAir is a graph • Arc between vertices • There is a flight between cities • Directed path • There is a sequence of flight connections

  3. A Non-Recursive Solution That Uses a Stack • The solution performs an exhaustive search • Beginning at the origin city, tries every possible sequence of flights until either • Finds a sequence that gets to the destination city • Determines that no such sequence exists • The ADT Stack is useful in organizing an exhaustive search • It helps you remember how you got the the current point • Backtracking can be used to recover from a wrong choice of a city

  4. X R R W R S P P P P W P DFS: Searching the Flight Map stk = newStack<E>(); stk.push(originCity); while (a sequence of flights from originCity to destinCity has not been found) { if (you cannot go anywhere from the city on top of stack) stk.pop(); // backtrack elseselect a neighbor, anotherCity, from the city on top of stack; stk.push(anotherCity); } W T T S S W W P P P P

  5. ... and remember where you’ve been stk = newStack<E>(); Clear Marks; stk.push(originCity); Mark(originCity) as visited; while (a sequence of flights from originCity to destinCity has not been found) { if (you cannot find an unvisited city from the city on top of stack) stk.pop(); // backtrack elseselect an unvisited neighbor, anotherCity, from the city on top of stack; stk.push(anotherCity); Mark(anotherCity) as visited; }

  6. P R X W S T Y Z X T Z R R W R S S S Y Y P P P P W W W W W W P P P P P P P Depth-First-Search Example: From P->Z List visited (marked) Stack stk P

  7. X R R P P Would DFS work for undirected graphs? Z stk = newStack<E>(); Clear Marks; stk.push(originCity); Mark(originCity) as visited; while (a sequence of flights from originCity to destinCity has not been found) { if (you cannot find an unvisited city from the city on top of stack) stk.pop(); // backtrack elseselect an unvisited neighbor, anotherCity, from the city on top of stack; stk.push(anotherCity); Mark(anotherCity) as visited; } Y W S R P T Q X P

  8. Labyrinth: Umberto Eco advises Theseus “To find the way out of a labyrinth there is only one means. At every new junction, never seen before, the path we have taken will be marked with three signs. If … you see that the junction has already been visited, you will make only one mark on the path you have taken. If all the apertures have already been marked, then you must retrace your steps. But if one or two apertures of the junction are still without signs, you will choose any one, making two signs on it. Proceeding through an aperture that bears only one sign, you will make two more, so that now the aperture bears three. All the parts of the labyrinth must have been visited if, arriving at a junction, you never take a passage with three signs, unless none of the other passages is now without signs.”

  9. C B A H E D F G E H C D D D G G B B B B B B B F F F A A A A A A A A A A Mazes as Graphs START EXIT C B A H E D F G A A

  10. Testing for Connectivity using DFS Z Connected: An undirected graph for which there is a path from any node to any other node Is this graph connected? Connected component: A connected sub-graph Can we use DFS to find all connected components? Y W S R P T Q X

  11. Strong Connectivity Strongly Connected: A graph for which there is a directed path from any node to any other node Is this graph strongly connected? Strongly connected component:A strongly connected sub-graph Can you find the strongly connected componentsof this graph?

  12. URL Access method Server and domain Path Document The Web is a Graph http://cs.wellesley.edu/~cs230/PPTs/Hash.html • Directed Graph of Nodes and Arcs • Nodes = web pages • Arcs = hyperlinks from a page to another • A graph can be explored • A graph can be indexed

  13. Syllabus Assignments Instructor CS Dept. Textbook Documentation PLTC ... ... ... ... ... ... The Web Graph - Starting at CS230 Home CS230

  14. Searching the Web The shape of the Web is … a “bow-tie”(!) • The web can be considered a graph “the web graph” • Web pages are the graph nodes • Hyperlinks on pages are graph edges • The web graph is huge (way over one million billions nodes) – maybe infinite (pages are created on the fly) • For the web graph, DFS is not a good strategy. (Why?) • You need to search your neighborhood before going deeper

  15. Breadth First Example 1 2 4 3 5 7 6 8 9 9 6 7 8 3 4 5 1 2 Queue: Iterator:

  16. BreadthFirstSearch(v) pseudocode a b • // BFS traversal starting at v • Initialization: • Mark all vertices as unvisited • enQueue v onto a new queue Q • Markv as visited • While (Q is not empty) • deQueue a vertex w from Q • For each unvisited vertex u adjacent to w: • enQueue u onto Q • Mark u as visited i c S e S G a h h f e G b i c f BFS from S to G:The BFS tree shows the visits How do you remember the path?

  17. How do you remember the path? a b i S c Sa Sh S[S] e Sh Sai Sab S G a [Sa] h h [Sh] Sai Sab She ShG f Sab She ShG e [She] G [ShG] b [Sab] i [Sai] She ShG Sabc Initialization: enqueue path [S] in Q While you have not reached G dequeue a path from BFS queue and check the last node x in the path extend the path to unvisited neighbors of x and enqueue extended paths to back of Q c [Sabc] f [ShGf] ShG Sabc … in the next step it will reach the goal G

  18. Dependency Graph on a DAG • Defined on a Directed Acyclic Graph (a “DAG”) • Usually reflect dependencies or requirements • I.e., Assembly lines, Supply lines, Organizational charts, … • Understanding dependencies requires “topological sorting” 232 231 225 235 110 230 240 114 215 111 251 242 315

  19. Resolving DAG Dependencies • Topological order • A list of vertices in a DAG such that vertex x precedes vertex y iff there is a directed edge from x to y in the graph • There may be several topological orders in a given graph • Topological sorting • Arranging the vertices into a topological order a b c d e f g

  20. b c d e f f c e b d g a A Topological Sorting Algorithm • Select a vertex vthat has no successor • Remove v from the graph (along with all edges that lead to it), • Add v to the beginning of a list of vertices L • Repeat previous steps • When the graph is empty, L’s vertices will be in topological order a g

  21. a b d e g c e b d a g f Another Topological Sorting Algorithm • Select a vertex vthat has no predecessor • Remove v from the graph (along with all edges that lead to it), • Add v to the end of a list of vertices L • Repeat previous steps • When the graph is empty, L’s vertices will be in topological order c f

More Related