Graphs & Algorithms: Class 17
Graphs & Algorithms: Class 17
Class 17
GRAPH
A Graph is the representation of a
topological relationship between objects by
making use of vertices and arcs (nodes and
edges)
Graph shows the topology
05/27/16
Representation of Graph
A graph G is a set of nodes V and edges E
G={V,E}
V={v1, v2,.,vn}
E={e1,e2,,em}
Edge ei between nodes vp and vq is given as
ei=(vp , vq)
05/27/16
Vp
EED, NIT Calicut
ei
Vq
3
Types of Graphs
Undirected Graph
Directed Graph
Weighted Graphs
Weighted Directed Graph
Weighted Undirected Graph
05/27/16
Classification
Directed- Edge has direction or orientation
shown using arrow heads
Undirected- no orientation
Weighted- Edge is associated with a number
05/27/16
V3
10
30
35
V2
12
40
V1
V4
25
50
V6
15
V5
V3
10
30
35
V2
12
40
V1
V4
25
50
V6
15
V5
V3
1
1
1
V2
1
V4
1
V1
V6
V5
V3
1
1
1
V2
1
V4
1
V1
V6
V5
05/27/16
10
Adjacency List
Two lists are maintained for adjacency of a
list
The first list is used to store information on
all the nodes
Second list is used to store information on
adjacent nodes
05/27/16
11
V1
V2
V3
05/27/16
V4
1
2
3
4
2
1
1
1
3
3
2
3
4
4
12
Adjacency Matrix
It is a two dimensional array with enties as
zeros and ones
Row and column corresponds to the
vertices(nodes)
05/27/16
13
A
B
C
D
05/27/16
A
0
0
1
1
B
0
0
1
0
C
1
1
0
1
D
1
0
1
0
14
Spanning Tree
A spanning tree is an undirected tree
Contains only just enough edges to connect
all the nodes
N Nodes means ST will have N-1 edges
Nodes of spanning tree will have only one
path between them
05/27/16
15
05/27/16
16
05/27/16
17
Definitions
Let G be a weighted Graph
The length of a path P is the sum of the
weights of the edges that build up that path
If there are k edges with weight w(e i ) in a
path P, the length w(P) of path P is
w(P)
i k 1
w (e )
i 0
05/27/16
18
Definitions-contd
The distance from a node(vertex) v to a
node u in G denoted by d(v,u) is the length
of a minimum length path(also called
shortest path) from v to u, if such a path
exists.
We can take d(v,u)=+ if there exists no
path at all from v to u in G
05/27/16
19
Definitions-contd
Let the graphs be without any negative
weight cycles, self loops, and parallel edges
between any pair of nodes.
Assume that all weights are nonnegative ,
that is w(e)0
05/27/16
20
Sample graph
6 vertices
8 Edges
V3
8 weights>0
10
35
V2
12
40
V1
05/27/16
30
V4
25
50
V6
15
V5
21
DIJKSTRAS Algorithm
For simplicity we consider
undirected weighted Graphs
Dijkstras algortihm uses a greedy
methods pattern
05/27/16
22
23
05/27/16
24
Example
V3
10
35
V2
12
40
V1
05/27/16
30
V4
25
50
V6
15
V5
25
Edge Relaxation
Define a label D[u] for each vertex u of G.
D[u] will always store the length of the best
path we have found so far from v to u.
Initially D[v]=0 and D[u]= + for each u v
05/27/16
26
Edge Relaxation-contd
We define a set C, which is the set of nodes
in order of their distances from v
Initially C= (empty /null set)
At each iteration of the algorithm, we select
a vertex u not in C, with smallest D[u]
label, and we pull u into C.
In the very first iteration we will, of course,
pull v into C
05/27/16
27
Edge Relaxation-contd
Once a new vertex u is pulled into C, we
then update the label D[z] of each vertex
that is adjacent to u and is outside C, to
reflect that there may be a new and better
way to get to z via u
05/27/16
28
Edge Relaxation-contd
This update operation is known as a
relaxation procedure
Because, it takes an old estimate and checks
of it can be improved to get closer to its true
value.
05/27/16
29
Edge Relaxation-contd
In the case of Dijkstras Algorithm, the
relaxation is performed for an edge (u,z)
such that we have computed a new value of
D[u] and wish to see if there is a better
value for D[z] using the edge(u,z)
05/27/16
30
Edge Relaxation-contd
The specific edge relaxation operation is as
follows:
if D[u]+w((u,z))<D[z] then
D[z]D[u]+w((u,z))
05/27/16
31
Stage 1
v=v3
0
V3
10
10
30
35
V2
35
12
40
+
05/27/16
V1
V4
25
50
V6
15
V5
+
32
Stage 2
0
V3
10
10
30
35
V2
35
12
40
22
05/27/16
V1
V4
30
25
50
V6
15
V5
+
33
Stage 3
0
V3
10
10
30
35
V2
35
12
40
22
05/27/16
V1
V4
30
25
50
V6
15
V5
45
34
Traversal on Graph
Graph has no root or first node. So, traversal can
begin from any node
In graph, only those nodes which accessible from
current node are traversed
Particular node can be visited repeatedly. Hence a
repository of visits have to maintained with each
node
There may be multiple paths from one node to
another node.
05/27/16
35
Search on Graph
Breadth First Search
Depth First Search
05/27/16
36
Breadth First
05/27/16
37
05/27/16
38
05/27/16
39
05/27/16
40
TSP
Imagine a traveling salesman who has to
visit each of a given set of cities by car.
What is the shortest route that will enable
him to do so and return home, thus
minimizing his total driving?
05/27/16
41
TSP
42
Line diagram
showing Optimal
Tour through
24,978 Cities in
Sweden
05/27/16
43
TSP as NP Complete
It belongs to a family of problems, called
NP-complete problem.
It is conjectured that all those problems
requires exponential time to solve them.
To find the optimal solution we have to go
through all possible routes, and the numbers
of routes increases exponentially with the
numbers of cities.
05/27/16
44
45
Types of algorithms
Deterministic Algorithm
Greedy Algorithm-Dijkstras Algorithm
Divide and Conquer Algorithm- Merge Sort
Heuristics Algorithm
Randomized Algorithms
Dynamic Programming
Approximation Algorithms
05/27/16
46
Deterministic Algorithm
Input
ALGORITHM
Output
47
Randomized Algorithm
A randomized algorithm is
an algorithm that employs a degree of
randomness as part of its logic.
05/27/16
48
Randomized Algorithms
Input
ALGORITHM
Output
Random Number
05/27/16
49
Randomized Algorithm
In addition to the input, the algorithm uses a
source of pseudo random numbers.
During execution, it takes random choices
depending on those random numbers.
The behavior (output) can vary if the
algorithm is run multiple times on the same
input.
05/27/16
50
05/27/16
51
Advantages of RA
The algorithm is usually simple and easy to
implement,
The algorithm is fast with very high
probability, and/or
It produces optimum output with very
high probability.
05/27/16
52
Shortcomings/Difficulties
There is a finite probability of getting
incorrect answer. However, the probability
of getting a wrong answer can be made
arbitrarily small by the repeated
employment of randomness.
Analysis of running time or probability of
getting a correct answer is usually difficult
05/27/16
53
Pseudorandom Numbers
Getting truly random numbers is
impossible.
One needs to depend on pseudo random
numbers.
So, the result highly depends on the quality
of the random numbers.
05/27/16
54
Randomized Quicksort
Given an input array A with two values p
and q as input to the Algorithm
We call it as RandQSORT(A,p,q)
A is to be sorted, p and q indices of the
locations
05/27/16
55
1: If p q, then EXIT.
2: While no central splitter has been found,
execute the following steps:
2.1: Choose uniformly at random a number r {p,
p + 1, . . . , q}.
2.2: Compute s = number of elements in A that are
less than A[r], and t = number of elements in A
that are greater than A[r].
2.3: If s (qp)/ 4 and t (qp)/ 4 , then A[r] is a
central splitter.
05/27/16
56
Contd
3: Position A[r] is A[s + 1], put the members
in A that are smaller than the central splitter in
A[p . . .s] and the members in A that are larger
than the central splitter in A[s + 2 . . . q].
4: RandQSORT(A, p, s);
5: RandQSORT(A, s + 2, q).
05/27/16
57
58
Thank You
Good Luck
05/27/16
59