Top Banner
CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms
19

CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.

CSC 2300Data Structures & Algorithms

March 30, 2007

Chapter 9. Graph Algorithms

Page 2: CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.

Today – Graphs

Graphs – Overview Definitions Representation

Topological Sort

Page 3: CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.

Graphs – Overview

Page 4: CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.

Definitions

Page 5: CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.

More Definitions

Page 6: CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.

Conventions

Page 7: CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.

Directed Graph – Example

Page 8: CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.

Graph Representations

1. Adjacency matrix

2. Adjacency list

Page 9: CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.

Adjacency Matrix

What does adjacency matrix look like? There are |E| edges and |V| vertices. How much space is used?

Page 10: CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.

Adjacency List – Example

There are |E| edges and |V| vertices. How much space is used?

Page 11: CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.

Two More Examples

Page 12: CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.

Graph Algorithms Overview

Page 13: CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.

Topological Sort

Page 14: CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.

Example – Course Prerequisites

Page 15: CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.

Example – Indegrees

What are the indegrees of the seven vertices? Is there a vertex with indegree = 0? Is it possible that there exists no vertex with indegree = 0?

Page 16: CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.

Topological Sort

Scan the array of vertices to look for a vertex with indegree 0 that has not been assigned a topological number.

There are |E| edges and |V| vertices. What is the running time of this algorithm? Is it possible to do better?

Page 17: CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.

Improved Topological Sort

Why always scan through all the vertices? Only a few vertices have their indegrees updated during

each iteration. We keep all (unassigned) vertices of indegree 0 in a queue. When a vertex v is removed from the queue, all vertices

adjacent to v will have their indegrees decremented. A vertex is placed in the queue if its indegree has fallen to 0.

There are |E| edges and |V| vertices. What is the running time of this new approach?

Page 18: CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.

Improved Topological Sort

Page 19: CSC 2300 Data Structures & Algorithms March 30, 2007 Chapter 9. Graph Algorithms.

Topological Sort – Example

How many iterations? Big Oh of what?