# CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS

Post on 21-Dec-2015

236 views

Embed Size (px)

TRANSCRIPT

<ul><li> Slide 1 </li> <li> CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS </li> <li> Slide 2 </li> <li> CSE 780 Algorithms Objectives At the end of this lecture, students should be able to: a. represent the graph b. write and analyze BFS algorithm. </li> <li> Slide 3 </li> <li> CSE 780 Algorithms Intro. Graph representations are very useful in a variety of disciplines. Examples of applications: Network analysis Project Planning Shortest route planning Compilers: Data-Dependency graphs Natural Language Processing </li> <li> Slide 4 </li> <li> CSE 780 Algorithms What Is A Graph Graph G = (V, E) V: set of nodes (vertices) E: set of edges Example: V ={ a, b, c, d, e, f } E ={(a, b), (a, d), (a, e), (b, c), (b, d), (b, e), (c, e), (e,f)} ba f c de </li> <li> Slide 5 </li> <li> CSE 780 Algorithms More Terminologies Directed / undirected graph Weighted / unweighted graph Special graphs Complete graph, planar (plane) graph, tree Size of graph: |V|, |E| |E| = O ( ) |V| 2 Degree of a node indegree / outdegree Node u and v are connected If there is a path from E connecting u and v </li> <li> Slide 6 </li> <li> CSE 780 Algorithms Properties of A Tree G = (V, E) is undirected graph, then is a tree has no cycle, and connected has |V|-1 edges, and connected </li> <li> Slide 7 </li> <li> CSE 780 Algorithms Representations of Graphs Adjacency lists An array of |V| lists Each vertex u has a list, recording its neighbors I.e., all vs such that (u, v) E </li> <li> Slide 8 </li> <li> CSE 780 Algorithms Adjacency Lists For vertex v V, its list has size: outdegree(v) decide whether (v, u) E or not in time O(outdegree(v)) Size of data structure (space complexity): O(|V| + |E|) </li> <li> Slide 9 </li> <li> CSE 780 Algorithms Adjacency Matrix </li> <li> Slide 10 </li> <li> CSE 780 Algorithms Adjacency Matrix Size of data structure: O ( |V| |V|) Time to determine If (v, u) E : O(1) Though larger, it is simpler compared to adjacency list. </li> <li> Slide 11 </li> <li> CSE 780 Algorithms Traverse of Graph Given graph G = (V, E) Given a source node s V Visit all nodes in V reachable from s If G is a tree Need an efficient strategy to achieve that BFS: Breadth-first search </li> <li> Slide 12 </li> <li> CSE 780 Algorithms Intuition Starting from source node s, Spread a wavefront to visit other nodes Example Imagine given a tree What if a graph? </li> <li> Slide 13 </li> <li> CSE 780 Algorithms Intuition cont. (u, v): Distance (smallest # edges) from node u to v in G A node: white: unvisited grey: discovered but not explored black: finished (explored) Goal: Start from source s, first visit those nodes of distance 1 from s, then 2, and so on. </li> <li> Slide 14 </li> <li> CSE 780 Algorithms More Formally BFS Input: Graph G=(V, E) and source node s V Output: (s, u) for every u V (s,u) = if u is unreachable from s Assume adjacency list representation for G Use FIFO queue Q to manage the traversal of vertices Keep track of the color of a vertex u using color[u]: </li> <li> Slide 15 </li> <li> CSE 780 Algorithms BFS (cont.) white not been discovered gray has been put on Q black its successors have been added to Q (explored) Keep track of the distance in d[u] Use [u] to store the predecessor of u. </li> <li> Slide 16 </li> <li> CSE 780 Algorithms Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. </li> <li> Slide 17 </li> <li> CSE 780 Algorithms Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. </li> <li> Slide 18 </li> <li> CSE 780 Algorithms Example </li> <li> Slide 19 </li> <li> CSE 780 Algorithms Analysis of Algorithm Initialise (Step 1 to 4): |V|-1 vertices are intialized to white in O(V). Queue Operations: Each enqueue and dequeu uses O(1) time, hence all queue operations take O(V) Scanning Adjacency Lists (step 12): The sum of the lists length is O(E). The time to scan the lists is O(E) Time complexity: O ( |V| + |E| ) </li> <li> Slide 20 </li> <li> CSE 780 Algorithms Breadth-First Tree v = (u) if u is first discovered while exploring v The set of edges ((u), u) for all u V form a tree The breadth-first tree with root s All nodes of the same level of tree has same d[] value Nodes are explored level by level </li> <li> Slide 21 </li> <li> CSE 780 Algorithms Print a shortest path from s to v Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. </li> <li> Slide 22 </li> <li> CSE 780 Algorithms Correctness of Algorithm A node not reachable from s will not be visited A node reachable from s will be visited d(u) computed is correct: (i.e., = (s, u) ) Proof by induction Consider the breadth-first tree generated by algorithm: Any node u from kth level: d[u] = k Claim: all nodes v with (s, v) = k are in level k and all nodes from level k has (s, v) = k Observation: if edge (u,v) exists, Then (s, v) (s, u) + 1 </li> <li> Slide 23 </li> <li> CSE 780 Algorithms Summary for BFS Works for both directed and undirected graph Starting from source node s, visits remaining nodes of graph from small distance to large distance Return distance between s to any reachable node in time O(|V| + |E|) Last updated: 13/02/2009 </li> </ul>

Recommended

View more >