1 / 23

Graphs

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.

Download Presentation

Graphs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graphs

  2. contents: • Elementary graph algorithms • Minimum spanning trees • Shortest path ­­- Dijstra algorithm

  3. ˙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.

  4. 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)

  5. ˙Path: sequence of vertices w1, w2,…, wN where (wi, wi+1)  E,  1 iN. ˙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.

  6. 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.

  7. ˙Drawback: O(V2) storage, more suitable for dense graph.

  8. Tradeoffs between Adjacency List and Matrix

  9. Breadth-First Search (BFS) ˙Breadth First search (BFS): level order tree traversal ˙BFS algorithm: using queue

  10. ˙ 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); } } }

  11. 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); }

  12. ˙white (undiscovered) → gray (discovered) → black (explored: out edges are all discovered)

  13. 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.

  14. 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

  15. 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

  16. 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

  17. 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|)

  18. 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

  19. 1 2 3 4 5 6 7 2 1 3 0 2 1 3

  20. 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

  21. 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

  22. THE END

More Related