Graphs 52
Graphs 52
• Directed/Undirected?
• Draw the graph
Another Graph
• Facebook Friend
• Twitter follower
• Using GPS/Google Maps/Yahoo Maps, to
find a route based on shortest route.
• Network Traffic flow/Shortest
Applications •
path/Minimum spanning tree
Google, to search for webpages, where
of Graphs: pages on the internet are linked to each
other by hyperlinks; each page is a vertex
and the link between two pages is an edge.
• On eCommerce websites relationship graphs
are used to show recommendations.
• Page Ranking
• Scientific Computation (Atom, Protein, etc)
• Website analysis
• Biological analysis
Spanning
Tree
• Want to send a message to each of the people on network
(no cycles)
• Spanning Tree
• Want to send a message to each of the people on network, by
cheapest method
• Minimum Spanning Tree
https://www.researchgate.net/figure/A-weighted-friend-network-graph-in-a-social-network_fig4_263353737
Spanning Tree
Scenario: you work for phone company and your
task is to provide fiber lines to a village with 10
houses, each labeled H1 through H10.
Specifically, this involves running a single cable that
connects each home. That is, the cable must run
through houses H1, H2, and so forth, up through
H10. Due to geographic obstacles—hills, trees,
rivers, and so on—it is not feasible to necessarily run
the cable from one house to another.
Diagram shows this problem depicted as a graph. Each node is a house, and the
edges are the means by which one house can be wired up to another. The weights of
the edges dictate the distance between the homes.
Your task is to wire up all ten houses using the least amount of cabling
possible. Ref:http://msdn.microsoft.com/en-us/library/ms379574.aspx
• A spanning tree of a connected graph is a
tree whose edges are in the graph and
that contains all the vertices in the graph
• A minimal spanning tree of a connected
network uses a spanning tree that has the
smallest possible weight sum
Spanning • Examples: we wish to connect all the
7
12 4
C D
6
A
Sample Spanning Trees
10
B
15
E
Gr a p h wi t h a r c we i g h
7
12
4
C 10 D
15
A 6 B E
Graph w it h arc w eight s .
7
12
4
C
6
DSpanning Tr e e s
Spanning Trees
10 15
A 10
B 15
E
A B E
7
7
12
1 2
C D
C D
10 15
A B E
10 15
12
A B E
C D
6
12
Minimum Spanning Tree
C D
6
10
A B E
M i n7
imum Spa nning Tr e e
4
C D
6
Sum a rc w e i g h ts = 27
10
A B E
7
4
C D
6
Minimum Spanning Tree
10
A B E
7
4
C D
6
Locations:
Minimal
Spanning Do not always give the
Tree optimal spanning tree
because the algorithms are
greedy ones rather than
full searches
Kruskal's Algorithm
• Edges are considered for inclusion in the tree in non-decreasing order of their
costs.
• An edge is included in the tree if it doesn't form a cycle with the edges already
in the tree.
5 6 E F
6
1
Prim's Algorithm
• An algorithm to find the minimal spanning tree starts with the
specified node and builds up the tree, adding one arc at a time.
• Choosing an arc with a smallest weight, which joins a node in the tree
to a node that has not yet been included.
Kruskals algorithm:
Prims Algorithm:
Shortest
Path
Shortest Path
• Interested in finding the cheapest route, regardless of the number of connections. This
might mean flying from New York to Miami, then Miami to Dallas, then Dallas to
Phoenix, Phoenix to San Diego, and finally San Diego to L.A.
• We can solve this problem by modeling the available flights and their costs as a directed,
weighted graph.
• What we are interested in knowing is what is the least expensive path from New York
to L.A.
• In this case, the arc weights are
considered to be a 'distance' between 2
nodes.
Path
and the new node.
•Dijkstra’s algorithm offers a way to find the
shortest paths from one vertex to all other
vertices
•It is like BFS except uses a priority queue.
Dijkstra’s Algorithm –PseudoCode
Input: a connected weighted graph in which all weights are positive w, Nodes a ,z.
Output: L(z), the length of a shortest path from a to z.
Dijkstra (w,a,z,L) {
L[a] = 0
For all nodes x ≠ a do
L[x] = ∞
T := set of all nodes // T is the set of all nodes whose shortest distance from a has not yet been
found
while z T do {
choose vT with minimum L(v)
T := T – {v}
For each xT adjacent to v do
L[x] := min( L[x], L[v] + w(v,x) )
}
}
Dijkstra's Algorithm.
• Greedy algorithm.
• If this has end point v, the node is deleted from the set of T
nodes (nodes not visited) and the L values are updated as
follows:
• For each arc (v,x) joining the new node v to node x (still in T
set), the element L[x] is redefined to be the smaller of
• it's current value, and
• the sum L[v] + weight(v,x)
• This value allows for a path from the start node to v passing
through the newly added node v.
Explanation!
• Greedy algorithm.
Input: a connected weighted graph in which all weights are positive w, Nodes a ,z.
• Node a is the start node, z is end node Output: L(z), the length of a shortest path from a to z.
• L[i] is a vector containing the shortest
distance from start node i known so far. If no Dijkstra (w,a,z,L) {
direct arc from a to i, then L[i] = ∞. L[a] = 0
• For all nodes x ≠ a do
The arc chosen at each stage is one whose
L[x] = ∞
end-point has the smallest value of L
T := set of all nodes
(length). (greedy)
while z T do {
choose vT with minimum L[v]
• If this has end point v, the node is deleted T := T – {v}
from the set of T nodes (nodes not visited) For each xT adjacent to v do
and the L values are updated as follows: L[x] := min( L[x], L(v) + w(v,x) )
• For each arc (v,x) joining the new node v to }
node x (still in T set), the element L[x] is }
redefined to be the smaller of
it's current value, and
the sum L[v] + weight(v,x)
• This value allows for a path from the start
node to v passing through the newly added
node v.
Dijkstra's Algorithm.
1
10
100
30
2 5
50
10 60
3 4
20
1
10
100 Dijkstra's
2
30
5 Algorithm.
50
10 60
3 4
20
Step v T L[1] L[2] L[3] L[4] L[5]
Init - {1,2,3,4,5} 0 ∞ ∞ ∞ ∞
1
10
100 Dijkstra's
2
30
5 Algorithm.
50
10 60
3 4
20
Step v T L[1] L[2] L[3] L[4] L[5]
Init - {1,2,3,4,5} 0 ∞ ∞ ∞ ∞
1 1 {2,3,4,5} 10 ∞ 30 100
1
10
100 Dijkstra's
2
30
5 Algorithm.
50
10 60
3 4
20
Step v T L[1] L[2] L[3] L[4] L[5]
Init - {1,2,3,4,5} 0 ∞ ∞ ∞ ∞
1 1 {2,3,4,5} 10 ∞ 30 100
2 2 {3,4,5} 60 30 100
1
10
100 Dijkstra's
2
30
5 Algorithm.
50
10 60
3 4
20
Step v T L[1] L[2] L[3] L[4] L[5]
Init - {1,2,3,4,5} 0 ∞ ∞ ∞ ∞
1 1 {2,3,4,5} 10 ∞ 30 100
2 2 {3,4,5} 60 30 100
3 4 {3,5} 50 90
1
10
100 Dijkstra's
2
30
5 Algorithm.
50
10 60
3 4
20
Step v T L[1] L[2] L[3] L[4] L[5]
Init - {1,2,3,4,5} 0 ∞ ∞ ∞ ∞
1 1 {2,3,4,5} 10 ∞ 30 100
2 2 {3,4,5} 60 30 100
3 4 {3,5} 50 90
4 3 {5} 60
1
10
100 Dijkstra's
2
30
5 Algorithm.
50
10 60
3 4
20
Step v T L[1] L[2] L[3] L[4] L[5]
Init - {1,2,3,4,5} 0 ∞ ∞ ∞ ∞
1 1 {2,3,4,5} 10 ∞ 30 100
2 2 {3,4,5} 60 30 100
3 4 {3,5} 50 90
4 3 {5} 60
5 5 {}
1
10
100 Dijkstra's
2
30
5 Algorithm.
50
10 60
3 4
20
Step 1 2 3 4 5
Init 0 ∞ ∞ ∞ ∞
1 1 10 ∞ 30 100
2 2 10 60 30 100
3 4 10 50 30 90
4 3 10 50 30 60
5 5 10 50 30 60
Dijkstra's Algorithm.
1
10
Shortest Path
2
30
5
from 1 :
1-2 : direct: 10
10 1-3 : 1-4-3: 50
3 4 1-4 : direct: 30
20
1-5 : 1-4-3-5: 60
Dijkstra's
Algorithm.
step a b c d e f g h i j k l m n o p q r s t u v w x y z α Ω
init
1
2
3
4
Shortest Path Problems
1. Dijkstra developed an algorithm which finds the shortest path of a
graph. Outline this algorithm and illustrate it usage on the following
graph which is stored as an adjacency matrix.
2. Trace your algorithm step by step from a vertex of your choice.
0 3 4 9 0 1
3 0 6 4 8 1
4 6 0 1 3 4
9 4 1 0 8 9
0 8 3 8 0 7
1 1 4 9 7 0
Use Dijkstra's algorithm to
determine the lengths of the
shortest paths from vertex A to
each other vertex of the graph with
the following adjacency matrix of
edge lengths.
Mazes
Mazes
“Graphs and mazes are one and the same
thing “.
http://www.cut-the-knot.org/ctk/Mazes.shtml
Mazes
Create the
maze using
Kruskal’s MST
algorithm
• Random weights
placed on edges
behind scenes
Or using Prim’s MST algorithm
http://www.cut-the-knot.org/ctk/Mazes.shtml