cse 780 algorithms advanced algorithms graph algorithms representations bfs
Post on 21-Dec-2015
367 views
TRANSCRIPT
CSE 780 Algorithms
Advanced Algorithms
Graph Algorithms Representations
BFS
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.
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
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
d e
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
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
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 v’s such that (u, v) E
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|)
CSE 780 Algorithms
Adjacency Matrix
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.
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
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?
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.
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]:
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.
CSE 780 Algorithms
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
CSE 780 Algorithms
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
CSE 780 Algorithms
Example
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| )
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
CSE 780 Algorithms
Print a shortest path from s to v
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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 k’th 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
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