CS2420: Lecture 36 - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

CS2420: Lecture 36

Description:

Weighted graphs and weighted digraphs (networks) have weights assigned to ... can pick up any ball and lift the entire graph without losing any other balls. ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 20
Provided by: Vladimir120
Category:

less

Transcript and Presenter's Notes

Title: CS2420: Lecture 36


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
5
A
B
7
1
4
C
D
2
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 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.

10
Topological Sorting Motivation
C4
C1
C3
C2
C5
11
Topological 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

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
C4
C1
C3
C2
C5
C1 is a source Delete C1 and its outgoing edges.
15
Topological Sorting Example
C4
C3
C2
C5
C2 is a source Delete C2 and its outgoing edges.
16
Topological Sorting Example
C4
C3
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)
Write a Comment
User Comments (0)
About PowerShow.com