Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
Graph AlgorithmsCptS 223
Getting from here to there...
What data structure would you use ?
A
B
C
D
E F
G
H
How do you even think about the
problem ?
Lets strip away the the irrelevant details..
Getting from here to there...
Vertices
A
B
C
D
E F
G
H
A
B
C
D
E F
G
H
Edges
Getting from here to there...
5
7
10
48
6
3
5
A
B
C
D
E F
G
H
Weights
Getting from here to there...
looks a lot more simpler now...
5
7
10
48
6
3
5
A
B
C
D
E F
G
H
Getting from here to there...
Graphs
• Are a tool for modeling real world problems.
• Allow us to abstract details and focus on the problem.
• We can represent our domain as graphs apply algorithms to solve our problem.
Simple Graphs
G = (V,E)
(u, v) ∈ E
u
v
Directed Graphs
(u, v) ∈ E
u
v
(v, u) !∈ E
Weighted Graphs
5
7
10
48
6
3
5
Path Cycle
Representation of graphs
A
B
C
A
B
C
A
B
C
B A
C
0 1 1
1 0 1
1 1 0
A
B
C
A B C
Adjacency Matrix Adjacency List
|V 2| |V | + |E|
Use Adjacency lists
forSparse Graphs
Topological Sort
A
B
C
D
E
F
G
H
A,B,D,C,E,F,G,H
A
C
B
No Cycles
Topological Sort
In degree = 3
Out degree = 1
Topological Sort
A
B
C
D
E
F
G
H
A,B,D,C,E,F,G,HO(|V |2)
Topological Sort
A
B
C
D
E
F
G
H
A,B,D,C,E,F,G,H
O(|V | + |E|)