4., Graphs-1
4., Graphs-1
4., Graphs-1
1 GRAPH
• A graph is an abstract data structure that is used to implement the mathematical
concept of graphs. It is basically a collection of vertices (also called nodes) and
edges that connect these vertices.
Graphs are widely used to model any situation where entities or things are
related to each other in pairs.
REAL WORLD EXAMPLE
On Facebook, everything is a node. That includes User, Photo, Album, Event, Group, Page,
Comment, Story, Video, Link, Note...anything that has data is a node.
Every relationship is an edge from one node to another. Whether you post a photo, join
a group, like a page, etc., a new edge is created for that relationship.
DEFINITION
• A graph G is defined as an ordered set (V, E), where V(G) represents
the set of vertices and E(G) represents the edges that connect these
vertices
V = {0, 1, 2, 3}
E = {(0,1), (0,2), (0,3), (1,2)}
G = {V, E}
GRAPH TERMINOLOGY
• When designing a graph we can make decisions as to:
• Use a directed graph or an undirected graph,
• Use a weighted graph or an unweighted graph.
An undirected graph is when edges have no A directed graph is when edges have a direction.
direction.
GRAPH TERMINOLOGY
A weighted graph is when edges have a numerical value A weighted and directed graph is where edges have a
direction and a numerical value!
GRAPH TERMINOLOGY
Adjacent nodes or neighbours:
For every edge, e = (u, v) that connects nodes u and v, the nodes u and
v are the end-points and are said to be the adjacent nodes or
neighbours
GRAPH TERMINOLOGY
Degree of a node:
• Degree of a node u, deg(u), is the total number of edges containing
the node u. If deg(u) = 0, it means that u does not belong to any edge
and such a node is known as an isolated node.
GRAPH TERMINOLOGY
Regular graph:
It is a graph where each vertex has the same number of neighbors. That
is, every node has the same degree. A regular graph with vertices of
degree k is called a k–regular graph or a regular graph of degree k.
GRAPH TERMINOLOGY
Path:
A path P written as P = {v0 , v1 , v2 , ..., vn ), of length n from a node u
to v is defined as a sequence of (n+1) nodes. Here, u = v0 , v = vn and
vi–1 is adjacent to vi for i = 1, 2, 3, ..., n.
Closed path :A path P is known as a closed path if the edge has the
same end-points. That is, if v0 = vn .
Simple path: A path P is known as a simple path if all the nodes in the
path are distinct with an exception that v0 may be equal to vn . If v0 =
vn , then the path is called a closed simple path.
GRAPH TERMINOLOGY
Cycle :
A path in which the first and the last vertices are same. A simple cycle
has no repeated edges or vertices (except the first and last vertices).
Connected graph:
A graph is said to be connected if for any two vertices (u, v) in V there
is a path from u to v. That is to say that there are no isolated nodes in a
connected graph. A connected graph that does not have any cycle is
called a tree. Therefore, a tree is treated as a special graph.
GRAPH TERMINOLOGY
Complete graph:
A graph G is said to be complete if all its nodes are fully connected.
That is, there is a path from one node to every other node in the graph. A
complete graph has n(n–1)/2 edges, where n is the number of nodes in G
GRAPH TERMINOLOGY
Clique:
In an undirected graph G = (V, E), clique is a subset of the vertex set C
Õ V, such that for every two vertices in C, there is an edge that connects
two vertices.
GRAPH TERMINOLOGY
Labelled graph or weighted graph:
A graph is said to be labelled if every edge in the graph is assigned some
data. In a weighted graph, the edges of the graph are assigned some
weight or length. The weight of an edge denoted by w(e) is a positive
value which indicates the cost of traversing the edge.
GRAPH TERMINOLOGY
Multiple edges :
Distinct edges which connect the same end-points are called multiple edges.
That is, e = (u, v) and e' = (u, v) are known as multiple edges of G.
Loop: An edge that has identical end-points is called a loop. That is, e = (u,
u).
1 1 6
4
2
6 5
1 1 6
4
2
6 5
8 3
2 3 4
Final Cost: 1 + 3 + 4 + 1 + 1 = 10
1 1 6
4
2
6 5
1
10 5
1
1
8 3
2 3 4
1 1 6
4
2
6 5
8 3
2 3 4
1 1 6
4
2
6 5
6 5
6 5
6 5
Union(1,3)
2 3 4
F= {{1,3},{2,5,6},{4}}
3 edges accepted
1
1
6 5
Do nothing
2 3 4
F= {{1,3},{2,5,6},{4}}
3 edges accepted
1
1
6 5
Union(1,4) 3
2 3 4
F= {{1,3,4},{2,5,6}}
4 edges accepted
1
1
6 5
(TSP)
66
A Salesman wishes to travel around a given set of cities,
and return to the beginning, covering the smallest total
distance
Easy to State
Difficult to Solve
67
If there is no condition to return to the beginning. It
can still be regarded as a TSP.
Suppose we wish to go from A to B visiting all cities.
A
B
68
If there is no condition to return to the beginning. It
can still be regarded as a TSP.
A
B
69
If there is no condition to return to the beginning. It
can still be regarded as a TSP.
A
B
70
A route returning to the beginning is known as a
Hamiltonian Circuit
71
Applications of the TSP
Routing around Cities
Computer Wiring - connecting together computer
components using minimum
wire length
Archaeological Seriation - ordering sites in time
4pm-7pm 6pm-7pm
Depot
8am-10am
6am-9am
2pm-3pm
73
Much more difficult than TSP
History of TSP
with Thanks
75
54
62
77
87
94
98
n=1512
n=13509
n=2392
n=7397
n=120
n=532
n=666
n=49
n=33
76
USA Towns of 500 or more population 13509 cities
1998 Applegate, Bixby, Chvátal and Cook
77
Towns in Germany 15112 Cities 2001Applegate,
Bixby, Chvátal and Cook
78
Sweden 24978 Cities 2004 Applegate, Bixby, Chvátal, Cook and Helsgaun
79
Solution Methods
I. Try every possibility (n-1)! possibilities – grows faster
than exponentially
80
The Nearest Neighbour Method (Heuristic)
– A ‘Greedy’ Method
1. Start Anywhere
4. Return to Beginning
81
A 42-City Problem The Nearest Neighbour Method
(Starting at City 1)
5
25 8
37
31
24 6 28 36
32
41
14 26 30
27
11
7
15
23
33 9
40
22 29
12
13
2 19 34
42 35
20
16
38
17 4 10
21
3
1 39
82
18
The Nearest Neighbour Method (Starting at City 1)
25 8
37
31
24 6 28 36
32
41
14 26 30
27
11
7
15
23
33 9
40
22 29
12
13
2 19 34
42 35
20
16
38
17 4 10
21
3
1 39
83
18
The Nearest Neighbour Method (Starting at City 1)
Length 1498
5
25 8
37
31
24 6 28 36
32
41
14 26 30
27
11
7
15
23
33 9
40
22 29
12
13
2 19 34
42 35
20
16
38
17 4 10
21
3
1 39
84
18
Remove Crossovers
25 8
31 37
24 6 28 36
32
41 26 30
14 27
11
7
15 23
40 33 9
22 29
12
13 19
2 34
42 35
20
16
38
17 4 21 10
3
1 39
85
18
Remove Crossovers
25 8
31 37
24 6 28 36
32
41 26 30
14 27
11
7
15 23
40 33 9
22 29
12
13 19
2 34
42 35
20
16
38
17 4 21 10
3
1 39
86
18
Remove Crossovers Length 1453
25 8
31 37
24 6 28 36
32
41 26 30
14 27
11
7
15 23
40 33 9
22 29
12
13 19
2 34
42 35
20
16
38
17 4 21 10
3
1 39
87
18
Christofides Method (Heuristic)
88
Christofides Method 42 – City Problem
Minimum Cost Spanning Tree
5 8
Length 1124
25
31
37
24
6 28
26 30 36
32
27
11
41 14
23 7 33
22
15 9
29
40 12
2
13 35
19
42 34
38
20
16
21 10
17 4
3
39
1
18
89
Minimum Cost Spanning Tree by Greedy
Algorithm
90
Match Odd Degree Nodes in Cheapest Way – Matching Problem
5 8
25
31
37
24
6 28
26 30 36
32
27
11
41 14
23 7 33
22
15 9
29
40 12
2
13 35
19
42 34
38
20
16
21 10
17 4
3
39
1
18
91
1. Create Minimum Cost Spanning Tree (Greedy
Algorithm)
92
Create a Eulerian Tour – Short Circuiting Cities revisited
Length 1436
5
8
25
31 37
24 28
6 32 36
41 26 30
14 27
11
7
15
23
40 33 9
22 29
12
13
2 19 34
42 35
20
16
38
17 4 10
21
3
1 39
93
18
Optimising Method
1.Make sure every city visited once and left once – in cheapest way (Easy)
-The Assignment Problem 5 - Results in subtours Length 1098
8
25
31 37
24 28
6 32 36
41 26 30
14 27
11
7
15
23
40 33 9
22 29
12
13
2 19
42 34 35
20
16
38
17 4 10
21
3
1 39
94
18
Put in extra constraints to remove subtours (More Difficult)
Results in new subtours Length 1154
5
8
25
31 37
24
6 28 32 36
41 26 30
14 27
11
7
15 23
40 33 9
22 29
12
13
2 19 34
42 35
20
16
38
17 4 21 10
3
1 39
95
18
Remove new subtours
Results in further subtours Length 1179
5
8
25
31 37
24 28
6 32 36
41 26 30
14 27
11
7
15
23
40 33 9
22 29
12
13 2 19
34 35
42
20
16
38
17 4 21 10
3
1 39
96
18
Further subtours Length 1189
8
25
31 37
24 28
6 32 36
41 26 30
14 27
11
7
15
23
40 33 9
22 29
12
13 2 19
34 35
42
20
16
38
17 4 21 10
3
1 39
97
18
Further subtours Length 1192
8
25
31 37
24 28
6 32 36
41 26 30
14 27
11
7
15
23
33 9
40
22 29
12
13 2 19
42 34 35
20
16
38
17 4 21 10
3
1 39
98
18
Further subtours Length 1193
8
25
31 37
24 6 28 36
32
41 26 30
14 27
11
7
15 23
33 9
40
22 29
12
13 2 19
34 35
42
20
16
38
17 4 10
21
3
1 39
99
18
Optimal Solution Length 1194
8
25
37
31
24 28
6 32 36
41 26 30
14 27
11
7
15
23
33
40 9
22 29
12
13 2 19
34 35
42
20
16
38
17 4 21 10
3
1 39
100
18