Applications of Graphs

advertisement
Applications of Graphs
 Topological Sorting
 Spanning Trees
 Minimum Spanning Tree
 Shortest Path Tree
 Circuits (cycles)
 Traveling Salesman Problem
Topological Sorting
could be courses,
showing prerequisites
Topological Sorting
possible sequence of courses ...
Spanning Trees
tree – undirected connected graph without cycles
spanning tree – subgraph of graph G that contains all edges of G
spanning tree with n vertices contains n-1 edges
Spanning Trees
Different spanning trees with 4 vertices and 3 edges
DFS Spanning Tree
BFS Spanning Tree
Minimum Spanning Tree (MST)
Assume the weight of an edge represents a cost value
What is the cheapest solution (tree) that connects all vertices?
Minimum Spanning Tree (MST)
Prim's Algorithm
start at any vertex (= root)
mark root as visited and add it to the MST
while (there are still unvisited vertices)
{
find the least-cost edge (v,u) from a visited vertex v
to some unvisited vertex u
mark u as visited
add vertex u and edge (v,u) to MST
}
MST Example
MST Example
MST Example
MST Example
MST Example
root
order of inclusion: a, i, f, g, d, h, c, e, b
Shortest Path Tree (SPT)
Assume the weight of an edge represents a distance value
What are the shortest paths from one root to all other vertices?
Shortest Path Tree (SPT)
Dijkstra's Algorithm
Dijkstra's Algorithm
B
D
4
1
1
1
A
F
2
3
C
1
E
shortest paths starting from node A
Dijkstra's Algorithm
B
D
4
1
1
1
A
F
2
3
C

1
E
start out with source node (= root)
Dijkstra's Algorithm
B
D
4
1
1
1
A
F
2
3
C


1
E
start out with source node (= root)
set up paths to all reachable nodes
Dijkstra's Algorithm

1
B
D
4
1
1
F 
1
A
2
3
C
2



1
E

start out with source node (= root)
set up paths to all reachable nodes
compute distances from root
Dijkstra's Algorithm

1
B
D
4
1
1
F 
1
A
2
3
C
2


1
E

select the node with smallest distance,
which is not yet in the set of covered nodes
add it to the set of covered nodes
Dijkstra's Algorithm
1
B
5
D
4
1
1
F 
1
A
2
3
C
2




1
E

select the node with smallest distance,
which is not yet in the set of covered nodes
add it to the set of covered nodes
set up paths to all reachable nodes
update distances from root
Dijkstra's Algorithm
1
B
5
D
4
1
1
F 
1
A
2
3
C
2




1
E
3
select the node with smallest distance,
which is not yet in the set of covered nodes
add it to the set of covered nodes
set up paths to all reachable nodes
update distances from root
Dijkstra's Algorithm
1
B
4
D
4
1
1
1
A
F
2
3
C
2




1
E
3
select the node with smallest distance,
which is not yet in the set of covered nodes
add it to the set of covered nodes
set up paths to all reachable nodes
update distances from root
6
Dijkstra's Algorithm
1
B
4
D
4
1
1
1
A
F
2
3
C
2




1
E
3
select the node with smallest distance,
which is not yet in the set of covered nodes
add it to the set of covered nodes
set up paths to all reachable nodes
update distances from root
5
Dijkstra's Algorithm
1
B
4
D
4
1
1
1
A
F
2
3
C
2

1
E
3
if all nodes are covered  finished
5
Shortest Path Tree
Easy ??
By inspection ??
How about this one?
Other Famous Applications
• Euler circuit
 pass through every edge once
• Hamilton circuit
 pass through every node once
• Traveling Salesman Problem
Hamilton cycle with minimum costs
• Three Utilities Problem
 planar graphs
• Coloring Problem
 color nodes so neighboring nodes have different colors
Download