04-implementation-handout.pdf - Plan CMPSCI 311...

This preview shows 1 out of 3 pages.

CMPSCI 311: Introduction to Algorithms Akshay Krishnamurthy and Andrew McGregor University of Massachusetts Last Compiled: January 31, 2018 Plan Review: Quiz 1 questions Breadth First Search Depth First Search Traversal Implementation and Running Time Traversal Applications Directed Graphs Recall Graph G = ( V, E ) Set of nodes V of size n Set of edges E of size m Adjacency List Representation Adjacency List Representation. Nodes numbered 1 , . . . , n . Adj [ v ] points to a list of all of v ’s neighbors. BFS Description Define layer L i = all nodes at distance exactly i from s . Layers L 0 = { s } L 1 = all neighbors of L 0 L 2 = all nodes with an edge to L 1 that don’t belong to L 0 or L 1 . . . L i +1 = nodes with an edge to L i that don’t belong to any earlier layer. L i +1 = { v : ( u, v ) E, u L i , v / ( L 0 . . . L i ) } DFS Descriptions Depth-first search: keep exploring from the most recently discovered node until you have to backtrack. DFS( u ) Mark u as "Explored" for each edge ( u, v ) incident to u do if v is not marked "Explored" then Recursively invoke DFS( v ) end if end for
Image of page 1

Subscribe to view the full document.

Traversal Implementations Maintain set of explored nodes and discovered Explored = have seen this node and explored its outgoing edges Discovered = the “frontier”. Have seen the node, but not explored its outgoing edges. Generic Graph Traversal Let A = data structure of discovered nodes Traverse ( s ) Put s in A while A is not empty do Take a node v from A if v is not marked “explored" then Mark v as “explored" for each edge ( v, w ) incident to v do Put w in A w is discovered end for end if end while Note: one part of this algorithm seems really dumb. Why?
Image of page 2
Image of page 3
You've reached the end of this preview.

{[ 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