1 / 14

Chapter 9: Graphs

Mark Allen Weiss: Data Structures and Algorithm Analysis in Java. Chapter 9: Graphs. Topological Sort. Lydia Sinapova, Simpson College. Topological Sort. Problem Definitions Basic Algorithm Example Complexity Improved Algorithm Complexity. Problem. P: Assemble pool table

lani
Télécharger la présentation

Chapter 9: 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. Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Chapter 9: Graphs Topological Sort Lydia Sinapova, Simpson College

  2. Topological Sort • Problem • Definitions • Basic Algorithm • Example • Complexity • Improved Algorithm • Complexity

  3. Problem P: Assemble pool table F: Carpet the floor W: Panel the walls C: Install ceiling How would you order these activities?

  4. F P S W E C Problem Graph S – start F – floor P – pool table E – end W – walls C - ceiling Some possible orderings: C W F P W C F P F P W C C F P W Important: P must be after F

  5. Topological Sort RULE: If there is a path from u to v, then v appears after u in the ordering.

  6. Graphs’ Characteristics directed otherwise (u,v) means a path from u to v and from v to u , cannot be ordered. acyclic otherwise u and v are on a cycle : u would precede v , v would precede u.

  7. V1 V2 V3 V4 The ordering may not be unique legal orderings V1, V2, V3, V4 V1, V3, V2, V4

  8. Some Definitions Degree of a vertex U: the number of edges (U,V) - outgoing edges Indegree of a vertex U: the number of edges (V,U) - incoming edges The algorithm for topological sort uses "indegrees" of vertices.

  9. Basic Algorithm • Compute the indegrees of all vertices • Find a vertex U with indegree 0 and print it (store it in the ordering) • If there is no such vertex then there is a cycle and the vertices cannot be ordered. Stop. • 3. Remove U and all its edges (U,V) from the graph. • 4. Update the indegrees of the remaining vertices. • Repeat steps 2 through 4 while there are vertices to be processed.

  10. V1 V2 V3 V4 V5 Indegrees V1 0 V2 1 V3 2 V4 2 V5 1 Example First to be sorted: V1 New indegrees: V2 0 V3 1 V4 1 V5 2 Possible sorts:V1, V2, V4, V3, V5; V1, V2, V4, V5, V3

  11. Complexity O(|V|2),|V| - the number of vertices. To find a vertex of indegree 0 we scan all the vertices - |V| operations. We do this for all vertices: |V|2 operations

  12. Improved Algorithm üFind a vertex of degree 0, üScan only those vertices whose indegrees have been updated to 0.

  13. Improved Algorithm • Compute the indegrees of all vertices • Store all vertices with indegree 0 in a queue. • Get a vertex U. • 4. For all edges (U,V)update the indegree of V, and put V in the queue if the updated indegree is 0. • Repeat steps 3 and 4 while the queue is not empty.

  14. Complexity |V| - number of vertices, |E| - number of edges. Operations needed to compute the indegrees: Adjacency lists: O(|E|) Matrix: O(|V|2) Complexity of the improved algorithm Adjacency lists: O(|E| + |V|), Matrix representation: O(|V|2) Note that if the graph is complete |E| = O(|V|2)

More Related