0% found this document useful (0 votes)
2 views54 pages

Graphs 52

Uploaded by

mqnr4hr4gx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views54 pages

Graphs 52

Uploaded by

mqnr4hr4gx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

Chapter 5

Graphs & Mazes


Learning outcomes
• Applications of Graphs:
– Minimum Spanning Tree, Shortest Path, Mazes

• Excellent visualization resource for graphs


– https://graphonline.ru/en/
• More resources - Dijkstra Shortest path
– https://www.cs.usfca.edu/~galles/visualization/Dijkstra.html
Adjacency Matrix

• 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

Trees computers in a new office building using


the least amount of cable.
• Or run water pipes to houses using the
least amount of piping.
• Or Visit all locations with least amount
travelling distance/time
• Etc.
Minimal Spanning Tree
For example: finding the cheapest way of linking the
nodes to each other.
e.g. Graph with arc weights
10 15
A B E

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

Sum arc weights = 27


On a trip to Venice, you plan to visit all the important
world heritage sites but are short on time.
To make your itinerary work, you decide to use Minimal
Spanning Tree algorithm

Locations:

Units: meters * 100


Prim’s algorithm and
Kruskal’s algorithm.

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.

E is set of all edges in G.


T = []
While T contains less than n-1 edges and E not empty
{
choose edge (v,w) from E of lowest cost

delete (v,w) from E

if (v,w) does not create a cycle in T then


add (v,w) to T
else
discard (v,w)
}
If T contains fewer than n-1 edges Output No Spanning Tree.
Exercises
1. 2.
3
1 A B
6
5
2
1 1 4
5 5
2 4 C 2 D
3
3 6 3
4 2 4

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.

included = [s] (* start node *)


Repeat
choose minimal edge (i,j) with i IN included
and j NOT IN included
add arc (i,j) to the tree
included = included + [j]
Until included contains all nodes.
Website References
• Minimal Spanning Tree
https://visualgo.net/en/mst

• Kruskals Spanning Tree


https://www.cs.usfca.edu/~galles/visualization/Kruskal.html

• Prim’s Spanning Tree


https://www.cs.usfca.edu/~galles/visualization/Prim.html
List the first six edges chosen for MST when using:

Kruskals algorithm:
Prims Algorithm:
Shortest
Path
Shortest Path

Flying New York to LA

• 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.

• The shortest path between 2 nodes is


defined as the path whose sum of arc
Shortest weights is minimal.

Path • On a network, there may be more than


one path from a certain node to another
one
• Therefore, the path with the smallest
weight sum is called the shortest path
between the two nodes.
•The minimal spanning tree selects a tree
with minimal overall sum of arc weights. This
doesn't guarantee that the path between the
given start node and any other node will be
the shortest path.
MST vs •An algorithm to find the shortest path

Shortest between the start node and any other node


examines the distance from the start node

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 vT with minimum L(v)
T := T – {v}
For each xT adjacent to v do
L[x] := min( L[x], L[v] + w(v,x) )
}
}
Dijkstra's Algorithm.
• Greedy algorithm.

• Node a is the start node, z is end node


• L[i] is a vector containing the shortest distance from start
node i known so far. If no direct arc from a to i, then L[i] = ∞.
• The arc chosen at each stage is one whose end-point has the
smallest value of L (length). (greedy)

• 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 vT 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 xT 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 “.

”…the apparent complexity of a maze


should not be judged by a naked eye, but
rather with the mind's eye”

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

Solve the ‘problem’


using DFS
Cells
‘Nodes’
Spanning Tree
Fill in Edges
Maze
Solve using DFS or BFS

You might also like