230 likes | 322 Views
Graphs. contents:. Elementary graph algorithms Minimum spanning trees Shortest path - Dijstra algorithm. ˙A graph G = ( V, E ) consists of a set V of vertices ( nodes ) and a set E of directed or undirected edges . ˙ Any binary relation is a graph.
E N D
contents: • Elementary graph algorithms • Minimum spanning trees • Shortest path - Dijstra algorithm
˙A graph G = (V, E) consists of a set V of vertices (nodes) and a set E of directed or undirected edges. ˙Any binary relation is a graph. Network of roads and cities, circuit representation, etc.
Terminology of Graph ˙Graph: G=(V, E), V: a set of vertices, E: a set of edges ˙Edge (arc): a pair (v,w), where v, w V ˙Directed graph (Digraph): graph if pairs are ordered (directed edge) ˙Adjacent: w is adjacent to v if (v,w) E ˙Undirected graph: if (v,w) E, (v,w)=(w,v)
˙Path: sequence of vertices w1, w2,…, wN where (wi, wi+1) E, 1 iN. ˙Length of a path: number of edges on the path. ˙Loop: an edge (v, v) from vertex to itself ˙Simple path: a path where all vertices are distinct except the first and last. ˙Cycle in a directed graph: a path such that w1 = wN ˙Acyclic graph (DAG):a directed graph which has no cycles.
Representations of Graphs: Adjacency List ˙Adjacency list: An array Adj of |V | lists, one for each vertex in V. For each u ∈ V, Adj[u] pointers to all the vertices adjacent to u. ˙Advantage: O(V+E) storage, good for sparse graph. ˙Drawback: Need to traverse list to find an edge.
Breadth-First Search (BFS) ˙Breadth First search (BFS): level order tree traversal ˙BFS algorithm: using queue
˙ white (undiscovered) → gray (discovered) → blue (explored: out edges are all discovered) ˙ Use queue Q for gray vertices. void BFS(Graph G); /* O(|E|+|V|) */ { queue Q; vertex V,W; Q=CreateQueue(NumVertex); MakeEmpty(Q); Visted[V]=True; Enqueue(V,Q); While ( !IsEmpty(Q) ) { V=Dequeue(Q); for each W adjacent to V if ( !Visited[W] ) { Visited[W]=True; Enqueue(W,Q); } } }
Depth-First Search (DFS) ˙Depth First Search: generalization of preorder traversal ˙Starting from vertex v, process v & then recursively traverse all vertices adjacent to v. ˙To avoid cycles, mark visited vertex void DFS(Vertex V) { Visited[V]=True; for each W adjacent to V if (!Visited[W]) DFS(W); }
˙white (undiscovered) → gray (discovered) → black (explored: out edges are all discovered)
Minimum Spanning Tree (MST) Spanning Tree:a tree formed from graph edges that connects all the vertices ˙Minimum Spanning Tree: spanning tree with lowest total cost ˙A minimum spanning tree exists iff G is connected ˙Given an undirected graph G = (V, E) with weights on the edges, a minimum spanning tree (MST) of G is a subset T ⊆ E such that T is connected and has no cycles, T covers (spans) all vertices in V, and sum of the weights of all edges in T is minimum.
2 2 1 2 1 2 10 4 1 1 3 2 2 7 3 4 5 3 4 5 4 8 4 5 6 6 6 7 6 7 1 1
2 2 1 2 10 4 1 1 3 2 2 7 3 4 5 4 8 4 5 6 6 6 7 1 1 Principle of Prim’s Algorithm • Greedy algorithm • At each stage, a new vertex is added to the tree • The new selected vertex v: smallest cost (u,v) where u in the tree, v is not. 1 2 3 4 5 6 7
2 2 1 2 10 4 1 1 3 2 2 7 3 4 5 4 8 4 5 6 6 6 7 1 1 Principle of Kruskal’s Algorithm • Greedy algorithm • At each stage, a new edge is added to the tree • The selected edge is the smallest cost among unselected edge and it does not cause a cycle. 1 2 3 4 5 6 7
Shortest-Path • weight graph: graph in which each edge (vi, vj) is associated with a cost ci,j • weighted path length • Shortest-Path problem • Single-Source Shortest-Path problem • Unweighted Shortest Paths O(|V|+|E|) • Weighted Shortest Paths with no Negative Edges O(|E|log|V|)
Unweighted Shortest Paths • Unweighted shortest paths: minimum no. of edges along path • Strategy of algorithm: • breadth-first search, processing vertices in layers • the vertices closest to the start are evaluate first • level order traversal of trees
1 2 3 4 5 6 7 2 1 3 0 2 1 3
Weighted Shortest Paths • Dijkstra’s algorithm: greedy algorithm • Greedy algorithm: solve a problem in stages by doing what appears to be the best choice at each stage. • Each stage of Dijkstra’s algorithm • selects a vertex v which has the smallest dv among unknown vertices and declare v as known • update dw S w v
1 2 2 4 1 3 10 2 2 3 4 5 5 8 4 6 6 7 1 * * 0 2 * * * 1 3 3 * * 8 9 6 5