0% found this document useful (0 votes)
3 views

Graphs 1

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)
3 views

Graphs 1

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/ 45

Searching – Graphs, Kruskal and

Dijkstra algorithms

1
Recap on graphs
• A graph in mathematics is not just a function plotted using
cartesian coordinates

• It can be a network of edges/connections between


vertices/dots
a V b h j
U X Z
d
c e i
W
g
f Y 2
Recap on graphs
• Vertices (or nodes or endpoints) of an edge
– U and V are the endpoints of a
• Edges incident on a vertex
– a, d, and b are incident on V V
• Degree of a vertex a b h j
– X has degree 5 U d X Z
• Parallel edges e i
c
– h and i are parallel edges W g
• Self-loop
f
– j is a self-loop Y
• Cycle
– circular sequence of alternating vertices and
edges where each edge is preceded and
followed by its endpoints (U- V- X- W)
3
Representations of unweighted
graphs

4
Graph Traversal
• An “exhaustive search” systematically processes all vertices and edges
of a graph
– Breadth-first search (BFS)
– Depth-first search (DFS)
• Very useful for many applications involving graphs in artificial
intelligence and operations research
• Indispensable for efficient investigation of fundamental properties of
graphs such as connectivity and cycle presence
• Important elementary applications of BFS and DFS include checking
connectivity and checking acyclicity of a graph.
– BFS: Social Networking Websites, GPS Navigation systems, Peer to Peer
Networks (for finding neighbouring friends, locations, nodes), Crawlers in
Search Engines
– DFS: Detecting cycle in a graph, Path Finding, Crawlers in Search
Engines
5
Breadth-First Search (BFS) - 1
• BFS starts at an arbitrary vertex by marking it as visited.
• BFS proceeds in a concentric manner by visiting first all unvisited
vertices that are adjacent to a starting vertex.
– (If there are several such vertices, …e.g. resolve ties by the vertex sorting order)
• BFS then all unvisited vertices two edges apart from it, and so on,
until all the vertices in the same connected component as the starting
vertex are visited.
• If there still remain unvisited vertices, the algorithm has to be restarted
at an arbitrary vertex of another connected component of the graph.

6
Breadth-First Search (BFS) - 2

7
Breadth-First Search (BFS) - 3

8
Depth-First Search (DFS) - 1
• DFS starts at an arbitrary vertex by marking it as visited.
• On each iteration, DFS proceeds to an unvisited vertex that is adjacent
to the one it is currently in.
– (If there are several such vertices, …e.g. resolve ties by the vertex sorting order)
• This process continues until a dead end—a vertex with no adjacent
unvisited vertices— is encountered.
• At a dead end, DFS backs up one edge to the vertex it came from and
tries to continue visiting unvisited vertices from there.
• DFS eventually halts after backing up to the starting vertex, with the
latter being a dead end. By then, all the vertices in the same connected
component as the starting vertex have been visited.
• If unvisited vertices still remain, the depth-first search must be
restarted at any one of them.

9
Depth-First Search (DFS) - 2

10
Depth-First Search (DFS) - 3

11
Depth-First Search (DFS) - 4

12
Depth-First Search (DFS) - 5

13
BFS vs DFS

BFS DFS

• BFS visits all the unvisited • DFS visits recursively all the unvisited
vertices connected to a vertex. vertices connected to a vertex.
• “cautious”. • “brave”.

14
The use of graphs in searching
• Weights can be distance, money, load, etc
• Digraph G = (V,E) with weight function W: E ® R
(assigning real values to edges)
• Weight of path p = v1 ® v2 ® … ® vk is
k1
w( p)  w(vi , vi 1 ) w(pAB)=1+4+10=15
i 1

B
vertex (V) 10 weight (W)
4
9
8 4
16 edge (E)
1
A 2
13 7 6 15
Representations of weighted
graphs

16
The purpose of Weights
• The weights associated with each edge can represent
– Cost (laying communications cabling)
– Time (airline flight times including waiting in transit)
– Distance (Transportation courier services)
– Data traffic (network routing including transfer/receive rate)
or any other quantity we are interested in measuring between vertices
• Once we have weights associated with the edges of a graph we
can try and minimise or maximise totals or take lowest or
highest weighted paths through the graph
• Can find the minimum accumulated weights to provide
– full connectivity (Krushkal’s algorithm)
– shortest route between two vertices (Dijkstra’s algorithm)

17
Full connectivity with Minimum
Spanning Trees - 1
• Suppose that G is a weighted graph. A minimum
spanning tree is a spanning tree of G which
minimises the weights on the edges in the tree.
• Kruskal's algorithm (uses a greedy algorithm)
finds a spanning tree with the minimum
accumulated weights
• There may be more than one spanning tree with this
smallest accumulated weight

18
Full connectivity with Minimum
Spanning Trees - 1
• Suppose that G is a weighted graph. A minimum
spanning tree is a spanning tree of G which
minimises the weights on the edges in the tree.
• Applications
– It has direct applications to the design of all kinds of networks—
including communication, computer, transportation, and electrical—
by providing the cheapest way to achieve connectivity.
– It identifies clusters of points in data sets.
– It has been used for classification purposes in archeology, biology,
sociology, and other sciences.

19
Full connectivity with Minimum
Spanning Trees - 2
• Kruskal's algorithm (uses a greedy algorithm)
finds a spanning tree with the minimum
accumulated weights
• There may be more than one spanning tree with this
smallest accumulated weight

20
Kruskal’s Algorithm

This method builds the graph as you go along


starting with just the vertices
Algorithm:
1. The graph T initially consists of the vertices of G
and no edges.
2. Add an edge to T having minimum weight but
which does not give a cycle in T.
3. Repeat step 2 until T has n - 1 edges.

21
Points to note about Kruskal’s alg
• You must arrange the edges in increasing size,
check you haven’t left any out.
• You must add the edges to the graph in the right
order, it may not connect at first
• If two edges are of the same weight it does not
matter which order you arrange them
• Not all answers will be the same but all minimal
spanning trees will give the same totals
• For a graph with |V| vertices the final tree should
have |V|-1edges
22
Kruskal’s Algorithm
Derby e6 Nottingham edge weight
e5 e11 e1 11 accept
e7
e9 accept
Ashby e2 12
e2 Loughborough Grantham e3 15 reject
e8
e3 e1 e10 e4 15 accept
e5 16 accept
Leicester e4 Melton Mowbray
e6
e6 16 accept
e7 16 reject
e11
e5 e7 e8 17 reject
e9
e9 18 reject
e2
e10 20 accept
e11 24 reject
e8
e3 e1 e10
The minimum length of cable is
11+12+15+16+16+20=90
23
e4
Searching in Graphs –
Dijkstra’s algorithm

24
Finding the shortest path in a graph

25
Finding the shortest path in a graph
• The single-source shortest-paths problem: for a given
vertex called the source in a weighted connected graph,
find shortest paths to all its other vertices.

• Applications
• The transportation planning and packet routing in communication
networks, including the Internet.
• Finding shortest paths in social networks, speech recognition,
document formatting, robotics, compilers, and airline crew
scheduling.
• Pathfinding in video games and finding best solutions to puzzles
using their state-space graphs.

26
Dijkstra’s algorithm
• Dijkstra’s algorithm finds the shortest paths to a graph’s
vertices in order of their distance from a given source.

• Dijkstra’s algorithm assumes that


• the graph is connected
• edges are undirected
• edge weights are non-negative

27
Dijkstra’s algorithm
1. Label the start vertex as the “boxed vertex” and has a permanent cost 0 .
All other vertices have undefined temporary costs.
2. Assign temporary accumulated costs to all the vertices that can be reached
directly from the boxed vertex. Note, if there is an existing temporary
accumulated cost at a vertex, it should be replaced only if the new cost is
smaller.
3. Select the vertex with the smallest temporary accumulated cost from all
unboxed vertices and make this vertex permanent by boxing it and store
the accumulated cost.
4. Using the newly boxed vertex, repeat steps 2-4 until all vertices have been
boxed.
5. To find the shortest path(s), trace back from the end vertex to the start
vertex. Write the route forwards and state the length.

28
Dijkstra’s algorithm
Problem:
Find the shortest path to drive from town (A) to
destination town (G). Weights are based on miles
B 4
F
4 1
2 4
7 D
A
7
3 2
3
G
2
C 5 E 29
Dijkstra’s algorithm
B 4
Label A as 0 and
F
box this number
4
1
2
4
0
7
D
A
7

3 2
3
G
2
C 5 E

30
Dijkstra’s algorithm
4A
B 4
F
4
1
2
7A 4
0
7
D
A
7

3 2
3
G
2
C 5 E
3A
We update each vertex adjacent to A
with a ‘working value’ for its distance
from A and indicate the route is from A
31
Dijkstra’s algorithm
4A
B 4
F
4
1
2
7A 4
0
7
D
A
7

3 2
3
G
2
C 5 E
3A
Vertex C is closest to
A so we give it a box.
32
Dijkstra’s algorithm
4A
B 4
6 < 7 so
replace the
F
4 label here
1
2
7A 6C 4
0
7
D
A
7

3 2
3
G
2
C 5 E
3A
8C
We update each vertex adjacent to C with a ‘working
value’ for its total distance from A, by adding its
distance from C to C’s permanent label of 3. 33
Dijkstra’s algorithm
4A
B 4
F
4
1
2
7A 6C 4
0
7
D
A
7

3 2
3
G
2
C 5 E
3A
8C

The vertex with the smallest


temporary label is B, so box this.
34
Dijkstra’s algorithm
4A 8B
B 4
5 < 6 so
replace the
F
4 label here
1
2
7A 6C 5B 4
0
7
D
A
7

3 2
3
G
2
C 5 E
3A
8C
We update each vertex adjacent to B with a ‘working
value’ for its total distance from A, by adding its
distance from B to B’s permanent label of 4. 35
Dijkstra’s algorithm
4A 8B
B 4
F
4
1
2
7A 6C 5B 4
0
7
D
A
7

3 2
3
G
2
C 5 E
3A
8C

The vertex with the smallest


temporary label is D, so box this. 36
Dijkstra’s algorithm
4A 8B 7D
B 4
F 7 < 8 so
4 replace the
1 label here
2
7A 6C 5B 4
0
7
D
A
7

3 2
3
7 < 8 so
replace the
G
label here 2 12D
C 5 E
3A
8C 7D
We update each vertex adjacent to D with a ‘working
value’ for its total distance from A, by adding its
distance from D to D’s permanent label of 5. 37
Dijkstra’s algorithm
4A 8B 7D
B 4
F
4
1
2
7A 6C 5B 4
0
7
D
A
7

3 2
3
G
2 12D
C 5 E
3A
8C 7D

The vertices with the smallest temporary


labels are E and F, so choose one and box it.
38
Dijkstra’s algorithm
4A 8B 7D
B 4
F
4
1
2
7A 6C 5B 4
0
7
D
A
7 9 < 12 so
replace the
3 2 label here
3
G
2 12D
C 5 E
9E
3A
8C 7D
We update each vertex adjacent to E with a ‘working
value’ for its total distance from A, by adding its
distance from E to E’s permanent label of 7. 39
Dijkstra’s algorithm
4A 8B 7D
B 4
F
4
1
2
7A 6C 5B 4
0
7
D
A
7

3 2
3
G
2 12D
C 5 E
9E
3A
8C 7D

The vertex with the smallest temporary


label is F, so make box this. 40
Dijkstra’s algorithm
4A 8B 7D
B 4
F
4
1
2
7A 6C 5B 4
0
7
D
A
7

3 2
3
11 > 9 so do
not replace G
2 the label 12D
C 5 E here
9
9E
3A
8C 7D

We update each vertex adjacent to F with a ‘working


value’ for its total distance from A, by adding its
41
distance from F to F’s permanent label of 7.
Dijkstra’s algorithm
4A 8B 7D
B 4
F
4
1
2
7A 6C 5B 4
0
7
D
A
7

3 2
3
G
2 12D
C 5 E
9
9E
3A
8C 7D G is the final vertex to
be boxed.
42
Dijkstra’s algorithm
4A 8B 7D
B 4
F
4
1
2
7A 6C 5B 4
0
7
D
A
7

3 2
3
G
2 12D
C 5 E
9
9E
3A
8C 7D
To find the shortest path from A to G, start from G and
work backwards, using the letters in the boxes. 43
The shortest path is ABDEG, with time of 9 miles.
Summary

• Looked at searching in graphs and how the search is based


on the nature of the problem being solved.

• Krushkal’s algorithm can be used when we need to find a


subset of the graph which still allows connectivity between
all vertices

• Dijkstra’s algorithm can be used to find the shortest path


between two vertices in a graph

44
Acknowledgements
• Some of this material has been taken from a variety of
sources
• the most notable of which is from
Tamassia, Goldwasser and Goodrich “Data Structures and
Algorithms in Python ” John Wiley & Sons.

• The baseline slides were created by Prof Constantinos


Ierotheou.

45

You might also like