Vous êtes sur la page 1sur 60

UMass Lowell Computer Science 91.

404
Analysis of Algorithms
Prof. Karen Daniels
Spring, 2007

Overview: Graph Algorithms


Chapter 22
Graph Algorithms

Introductory Graph Concepts

A B A B
C C

D F D E F
E
Introductory Graph Concepts:
Motivation

Introductory Graph Concepts:


Representations

A B A B
C C
D E F D E F

0 1 1 0 0 0
0 1 1 0 0 0
1 0 1 0 1 1
0 0 1 0 1 1
1 1 0 0 0 0
0 0 0 0 0 0
0 0 0 0 1 0
0 0 0 1 0 0
0 1 0 1 0 1
0 1 0 1 0 0
0 1 0 0 1 0
0 0 0 0 1 0
Introductory Graph Concepts:
Paths, Cycles
A B
C
D E F

A B
C
D E F

A B
C
D E F

Introductory Graph Concepts:


Connectivity A B
C
D E F

A B
C
D E F

A B
C
D E F
A B
C
D E F
Depth-First Search (DFS)
&
Breadth-First Search (BFS)

Depth-First Search (DFS)


Example:
DFS of Directed Graph

DFS PseudoCode
Vertex Color Changes

Edge Classification
Example: (continued)
DFS of Directed Graph

Example: (continued)
DFS of Directed Graph
Example: (continued)
DFS of Directed Graph

Example: (continued)
DFS of Directed Graph
Example: (continued)
DFS of Directed Graph

Example: (continued)
DFS of Directed Graph
Example: (continued)
DFS of Directed Graph

Example: (continued)
DFS of Directed Graph
Example: (continued)
DFS of Directed Graph

Example: (continued)
DFS of Directed Graph
Example: (continued)
DFS of Directed Graph

Example: (continued)
DFS of Directed Graph
Example: (continued)
DFS of Directed Graph

Example: (continued)
DFS of Directed Graph
Example: (continued)
DFS of Directed Graph

Example: (continued)
DFS of Directed Graph
Example: (continued)
DFS of Directed Graph

Example: (continued)
DFS of Directed Graph
Example: (continued)
DFS of Directed Graph

Example: (continued)
DFS of Directed Graph
Example: (continued)
DFS of Directed Graph

Example: (continued)
DFS of Directed Graph
Example: (continued)
DFS of Directed Graph

Example: (continued)
DFS of Directed Graph
Example: (continued)
DFS of Directed Graph

Example: (continued)
DFS of Directed Graph
Example: (continued)
DFS of Directed Graph

Example: (continued)
DFS of Directed Graph
Example:
DFS of Undirected Graph

Example:
DFS of Undirected Graph
Example:
DFS of Undirected Graph

Example:
DFS of Undirected Graph
Example:
DFS of Undirected Graph

Example:
DFS of Undirected Graph
Example:
DFS of Undirected Graph

Example:
DFS of Undirected Graph
Example:
DFS of Undirected Graph

Example:
DFS of Undirected Graph
Example:
DFS of Undirected Graph

Example:
DFS of Undirected Graph
Example:
DFS of Undirected Graph

Example:
DFS of Undirected Graph
Example:
DFS of Undirected Graph

Example:
DFS of Undirected Graph
Example:
DFS of Undirected Graph

Example:
DFS of Undirected Graph
Example:
DFS of Undirected Graph

Example:
DFS of Undirected Graph
Example:
DFS of Undirected Graph

Example:
DFS of Undirected Graph
Example:
DFS of Undirected Graph

Example:
DFS of Undirected Graph
Example:
DFS of Undirected Graph

Example:
DFS of Undirected Graph
Example: (continued)
DFS of Undirected Graph

Elementary Graph Algorithms:


DFS

A
A B Tree Edge
Back
C
Edge Tree Edge
F
B Tree Edge
E Tree Edge
D E F
C Cross Edge
Tree Edge

D
Breadth-First Search (BFS)

BFS PseudoCode
Example:
BFS of Directed Graph

Example:
BFS of Directed Graph
Example:
BFS of Directed Graph

Example:
BFS of Directed Graph
Example:
BFS of Directed Graph

Example:
BFS of Directed Graph
Example:
BFS of Directed Graph

Example:
BFS of Directed Graph
Example:
BFS of Directed Graph

Example:
BFS of Directed Graph
Example:
BFS of Directed Graph

Example:
BFS of Directed Graph
Example:
BFS of Directed Graph

Example:
BFS of Directed Graph
Example:
BFS of Directed Graph

Example: (continued)
BFS of Directed Graph
Example:
BFS of Undirected Graph

Example:
BFS of Undirected Graph
Example:
BFS of Undirected Graph

Example:
BFS of Undirected Graph
Example:
BFS of Undirected Graph

Example:
BFS of Undirected Graph
Example:
BFS of Undirected Graph

Example:
BFS of Undirected Graph
Example:
BFS of Undirected Graph

Example:
BFS of Undirected Graph
Example:
BFS of Undirected Graph

Example:
BFS of Undirected Graph
Example:
BFS of Undirected Graph

Example:
BFS of Undirected Graph
Example:
BFS of Undirected Graph

Example: (continued)
BFS of Undirected Graph
Depth-First Search (DFS)
&
Breadth-First Search (BFS)

Depth-First Search (DFS)


&
Breadth-First Search (BFS)
Using the Results of DFS & BFS

Using the Results of DFS & BFS


Running Time Analysis

∑ time to construct DFS tree i


i =1

ri

∑ AdjList[ jth vertex in DFS tree i]


j =1
Running Time Analysis
ri
t
∑ ∑ AdjList[ jth vertex in DFS tree i]
i =1
j =1

O (| V | + | E |)

Topological Sort
Definition: DAG

Definition: Topological Sort


Elementary Graph Algorithms:
Topological Sort

for Directed, Acyclic Graph (DAG)


G=(V,E)

Topological Sort
Example

Example
Topological Sort

Topological Sort

Vous aimerez peut-être aussi