Title: CS2420: Lecture 36
1CS2420 Lecture 36
- Vladimir Kulyukin
- Computer Science Department
- Utah State University
2Outline
- Graph Algorithms (Chapter 9)
3Weighted Graphs
- Weighted graphs and weighted digraphs (networks)
have weights assigned to their edges. - Weighted graphs can be represented as adjacency
matrices or adjacency lists.
4Weighted Graphs Example
5
A
B
7
1
4
C
D
2
5Paths
- 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.
6Cycles
- 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.
7Graphs 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.
8Topological 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.
9Topological Sorting Motivation
- There are five required courses for a student to
take. - The courses are C1, C2, C3, C4, and C5.
- C1 and C2 have no prerequisites.
- C3s prerequisites are C1 and C2.
- C4 has C3 as its prerequisite.
- C5 has C3 and C4 as its prerequisites.
10Topological Sorting Motivation
C4
C1
C3
C2
C5
11Topological Sorting
- Edges
- (C1, C3), (C2, C3), (C3, C4), (C3, C5), (C4, C5)
- This graph has two possible topological sortings
- C1, C2, C3, C4, C5
- C2, C1, C3, C4, C5
12Topological 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.
13Topological 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 -
14Topological Sorting Example
C4
C1
C3
C2
C5
C1 is a source Delete C1 and its outgoing edges.
15Topological Sorting Example
C4
C3
C2
C5
C2 is a source Delete C2 and its outgoing edges.
16Topological Sorting Example
C4
C3
C5
C3 is a source Delete C3 and its outgoing edges.
17Topological Sorting Example
C4
C5
C4 is a source Delete C4 and its outgoing edges.
18Topological Sorting Example
C5
C5 is a source, and we are done.
19Topological Sort Run Time