1 / 26

WEEK 8 Graphs I Definitions , Topological Sorting

WEEK 8 Graphs I Definitions , Topological Sorting. CE222 – Data Structures & Algorithms II Chapter 9.1, 9.2 (based on the book by M. A. Weiss, Data Structures and Algorithm Analysis in C++, 3rd edition, 2006). OUTLINE. Definitions Representation of Graphs Topological Sorting.

talasi
Télécharger la présentation

WEEK 8 Graphs I Definitions , Topological Sorting

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. WEEK 8Graphs IDefinitions , Topological Sorting CE222 – Data Structures & Algorithms II Chapter 9.1, 9.2 (based on the book by M. A. Weiss, Data Structures and Algorithm Analysis in C++, 3rd edition, 2006)

  2. OUTLINE • Definitions • Representation of Graphs • Topological Sorting CE 222-Data Structures & Algorithms II, Izmir University of Economics

  3. Definitions - I • A graph G=(V, E) consists of a set of vertices, V, and a set of edges, E. • Each edge is a pair (v, w), where v, w є V. • If the pair is ordered then G is directed (digraph). • Vertex w is adjacent to v iff (v, w) є E. • In an undirected graph with edge (v, w), w is adjacent to v and v is adjacent to w. • Sometimes and edge has a third component, weight or cost. CE 222-Data Structures & Algorithms II, Izmir University of Economics

  4. Definitions - II • A path in a graph is w1, w2,...,wN such that (wi, wi+1) є E for 1≤i<N. • The length of such a path is the number of edges on the path. Example: Path from 1 to 4  Nodes on path 1-2-3-4  Lenght is 3 • If a path from a vertex to itself contains no edges, then the path length is zero. CE 222-Data Structures & Algorithms II, Izmir University of Economics

  5. Definitions - III • If G contains an edge (v, v), then the path v, v is called a loop. • A simple path is a path such that no vertices are repeated, except that the first and the last could be the same. Example : The path 1-2-3-6-2-1 is not a simple path The path 1-6-3-5-2-1 is a simple path CE 222-Data Structures & Algorithms II, Izmir University of Economics

  6. Definitions - IV • A cycle in a directed graph is a path of length at least 1 such that w1=wN. • The cycle is simple if the path is simple. • For undirected graphs, • the edges in the cycle • are required to be distinct • (Why?). • A directed graph is acyclic if it has no cycles (DAG). CE 222-Data Structures & Algorithms II, Izmir University of Economics

  7. Definitions - V • An undirected graph is connected if there is a path from every vertex to every other vertex. • A directed graph with this property is called strongly connected. • If directed graph is not, but underlying undirected graph is, it is weakly connected. • A complete graph is a graph in which there is an edge between every pair of vertices. CE 222-Data Structures & Algorithms II, Izmir University of Economics

  8. Definitions - VI • A directed graph with this property is called strongly connected. • If directed graph is not, but underlying undirected graph is, it is weakly connected. • A complete graph is a graph in which there is an edge between every pair of vertices. • Example: • Not stronglyconnected • A-B, A-B-C, A-B-D,A-E • B-A, B-C, B-D ……. • But weaklyconnected CE 222-Data Structures & Algorithms II, Izmir University of Economics

  9. Representation of Graphs - I • One simple way is to use a two-dimensional array (adjacency matrix representation). If vertices are numbered starting at 1, A[u][v]=true if (u, v) є E. Space requirement is Θ(|V|2). Example : Edges (A,B) (A,E) (B,D) (B,C) (D,C) (E,B) CE 222-Data Structures & Algorithms II, Izmir University of Economics

  10. Representation of Graphs - II Ifthegraph is not dense (sparse), adjacencylistsmay be used. Thespacerequirement is O(|E|+|V|). CE 222-Data Structures & Algorithms II, Izmir University of Economics

  11. Topological Sort - I • There are many problems involving a set of tasks in which some of the tasks must be done before others. • Example: • Consider the problem of taking a course only after taking its prerequisites. • Is there any systematic way of linearly arranging the courses in the order that they should be taken?  TOPOLOGICAL SORT • A topological sort is an ordering of vertices in a DAG, such that if there is path from vi to vj, then vj appears after vi in the ordering. CE 222-Data Structures & Algorithms II, Izmir University of Economics

  12. Topological Sort - II Topological sort is a method of arranging the vertices in a directed acyclic graph (DAG), as a sequence, such that no vertex appear in the sequence before its predecessor. Topological sort is not unique. The following are all topological sort of the graph below: CE 222-Data Structures & Algorithms II, Izmir University of Economics

  13. Topological Sort - III A simplealgorithmtofind a topologicalordering : Findanyvertexwith no incomingedges. Printthisvertex, andremove it, alongwithitsedges. Thenapplythesamestrategytothe rest of thegraph. Toformalizethis, define theindegree of a vertex v as thenumber of edges (u, v). CE 222-Data Structures & Algorithms II, Izmir University of Economics

  14. TopologicalSort – InitialAttempt CE 222-Data Structures & Algorithms II, Izmir University of Economics

  15. TopologicalSort – InitialAttempt Running time of the algorithm is O(|V|2) |V| is NUM_VERTICES CE 222-Data Structures & Algorithms II, Izmir University of Economics

  16. TopologicalSort – A BetterAlgorithm I • Remove the inefficiency by keeping all the unassigned vertices of indegree 0 in a special data structure (queue or stack). • When a new vertex with degree zero is needed, it is returned by removing one (dequeue) from the queue • When the indegrees of adjacent vertices are decremented, they are inserted into the queue if the indegree falls to zero. The running time is O(|E|+|V|) CE 222-Data Structures & Algorithms II, Izmir University of Economics

  17. TopologicalSort – A BetterAlgorithm II CE 222-Data Structures & Algorithms II, Izmir University of Economics

  18. TopologicalSort – A BetterAlgorithm CE 222-Data Structures & Algorithms II, Izmir University of Economics

  19. Topological Sort Example The job consists of 10 tasks with the following precedence rules: Must start with 7, 5, 4 or 9. Task 1 must follow 7. Tasks 3 & 6 must follow both 7 & 5. 8 must follow 6 & 4. 2 must follow 4. 10 must follow 2. Make a directed graph and then a list of ordered pairs that represent these relationships. CE 222-Data Structures & Algorithms II, Izmir University of Economics

  20. Must start with 7, 5, 4 or 9. Task 1 must follow 7. Tasks 3 & 6 must follow both 7 & 5. 8 must follow 6 & 4. 2 must follow 4. 10 must follow 2. 9 1 7 3 5 6 8 4 10 2 Tasks listed as ordered pairs: 7,1 7,3 7,6 5,3 5,6 6,8 4,8 4,2 2,10 CE 222-Data Structures & Algorithms II, Izmir University of Economics

  21. 1 1 10 2 0 8 2 0 3 6 2 0 2 0 1 Predecessor Counts (Indegree) Successors (Linked list implementation of Graph) 1 1 2 2 3 3 4 4 5 5 6 6 8 7 7 1 3 6 8 8 9 9 10 10 Tasks listed as ordered pairs: 7,1 7,3 7,6 5,3 5,6 6,8 4,8 4,2 2,10 CE 222-Data Structures & Algorithms II, Izmir University of Economics

  22. Place all tasks that have zero predecessors(indegree) in a “queue”. Step 1. Remove any task from the queue and place in sorted list (Beginning with the value 1 assign an increasing topNum to each removed task): . Sorted List : 4, Step 2. Update Predecessor and Successor arrays as needed. CE 222-Data Structures & Algorithms II, Izmir University of Economics

  23. 10 8 2 3 6 1 3 6 PredecessorCounts (Indegrees) Successors (Linked list implementation of Graph) 1 1 1 2 0 2 3 2 3 4 0 4 0 5 5 6 2 6 8 7 0 7 8 1 8 9 0 9 10 1 10 CE 222-Data Structures & Algorithms II, Izmir University of Economics

  24. Step 3. Add to queue any tasks that now have zero predecessors (zero indegrees). Step. 4. Repeat steps 1, 2, and 3 until (all predecessor counts are all 0 ) or (until topNum=total number of tasks) or (queue becomes empty) (We are performing a “loop” or repetition.) CE 222-Data Structures & Algorithms II, Izmir University of Economics

  25. A Simple Example Using Queues #include <queue> #include <iostream> using namespace std; int main() { queue<int> Q; // FIFO push = enqueue pop= dequeue Q.push(1); Q.push(2); Q.push(3); Q.push(4); cout << "Size of queue is now : "<<Q.size()<<endl; while(Q.size()!=0) { Q.pop(); cout << "Size of queue is now : "<<Q.size()<<endl; cout << "First element to be popped in queue "<<Q.front()<<endl; cout << "Last element that has been pushed to the queue "<<Q.back()<<endl;} Q.push(5); cout << "Size of queue is now : "<<Q.size()<<endl; return 1; } CE 222-Data Structures & Algorithms II, Izmir University of Economics

  26. Homework Assignments • 9.1, 9.2, 9.3, 9.4, 9.38 • You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me. Izmir University of Economics

More Related