Download presentation
Presentation is loading. Please wait.
1
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University
2
Outline Graph Algorithms (Chapter 9)
3
Weighted Graphs Weighted graphs and weighted digraphs (networks) have weights assigned to their edges. Weighted graphs can be represented as adjacency matrices or adjacency lists.
4
Weighted Graphs: Example AB CD 1 2 4 7 5
5
Paths A path from vertex u to vertex v in a graph G is a sequence of adjacent (connected by an edge) vertices that starts with u and ends in v. If all vertices in a path are distinct (except possibly for the first and last one), the path is simple. The length of the path is the number of edges in the path.
6
Cycles A cycle is a simple path of positive length that starts and ends in the same vertex. A graph with cycles is cyclic. A graph without cycles is acyclic. DAG stands for directed acyclic graph.
7
Graphs: Connectivity An undirected graph G is connected if, for every pair of vertices u and v, there is a path from u to v. A directed graph with the same property is called strongly connected. If every vertex is a ball and every edge is a string, one can pick up any ball and lift the entire graph without losing any other balls.
8
Topological Sorting Let G = (V, E) be a directed acyclic graph (dag). Find a list of vertices such that for every edge (u, v) in E, u is listed before v.
9
Topological Sorting: Motivation There are five required courses for a student to take. The courses are: C 1, C 2, C 3, C 4, and C 5. C 1 and C 2 have no prerequisites. C 3 ’s prerequisites are C 1 and C 2. C 4 has C 3 as its prerequisite. C 5 has C 3 and C 4 as its prerequisites.
10
Topological Sorting: Motivation C1 C3 C2 C4 C5
11
Topological Sorting Edges: –(C 1, C 3 ), (C 2, C 3 ), (C 3, C 4 ), (C 3, C 5 ), (C 4, C 5 ) This graph has two possible topological sortings: –C 1, C 2, C 3, C 4, C 5 –C 2, C 1, C 3, C 4, C 5
12
Topological Sorting: Algorithm A source is a vertex with no incoming edges, i.e. the indegree of the source is 0. –Identify a source. –Delete it with all its outgoing edges from the graph. –Keep going until no more sources are left. –If, at any point, a source cannot be found, stop and return false. –Make sure that all vertices are included in the topological sorting.
13
Topological Sorting: Pseudocode TopologicalSort(G) { Initialiaze Q; int counter = 0; for each vertex V in G if ( V.indegree == 0 ) Q.Push(V); while ( !Q.IsEmpty() ) { V = Q.Pop(); V.TopNum = ++counter; for each W adjacent to V { W.indegree--; if ( W.indegree == 0 ) { Q.Pop(W); } if ( counter != NUM_VERTICES ) return an error, because G has a cycle; }
14
Topological Sorting: Example C1 C3 C2 C4 C5 C1 is a source; Delete C1 and its outgoing edges.
15
Topological Sorting: Example C3 C2 C4 C5 C2 is a source; Delete C2 and its outgoing edges.
16
Topological Sorting: Example C3 C4 C5 C3 is a source; Delete C3 and its outgoing edges.
17
Topological Sorting: Example C4 C5 C4 is a source; Delete C4 and its outgoing edges.
18
Topological Sorting: Example C5 C5 is a source, and we are done.
19
Topological Sort: Run Time O(|V| + |E|)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.