# CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.

Post on 21-Dec-2015

226 views

TRANSCRIPT

- Slide 1
- CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS
- Slide 2
- 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.
- Slide 3
- 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
- Slide 4
- 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
- Slide 5
- 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
- Slide 6
- 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
- Slide 7
- 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
- Slide 8
- 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|)
- Slide 9
- CSE 780 Algorithms Adjacency Matrix
- Slide 10
- 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.
- Slide 11
- 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
- Slide 12
- 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?
- Slide 13
- 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.
- Slide 14
- 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]:
- Slide 15
- 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.
- Slide 16
- CSE 780 Algorithms Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
- Slide 17
- CSE 780 Algorithms Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
- Slide 18
- CSE 780 Algorithms Example
- Slide 19
- 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| )
- Slide 20
- 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
- Slide 21
- CSE 780 Algorithms Print a shortest path from s to v Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
- Slide 22
- 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
- Slide 23
- 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

Recommended

View more >