0% found this document useful (0 votes)
37 views59 pages

Graphs & Algorithms: Class 17

The document discusses graphs and algorithms. It defines graphs as representations of topological relationships using vertices and edges. Different types of graphs are described such as directed, undirected, weighted graphs. Algorithms for graphs including Dijkstra's single source shortest path algorithm and algorithms for searching graphs like breadth first search and depth first search are explained. The traveling salesman problem is presented as an NP-complete problem to find the shortest route visiting all vertices of a graph once.

Uploaded by

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

Graphs & Algorithms: Class 17

The document discusses graphs and algorithms. It defines graphs as representations of topological relationships using vertices and edges. Different types of graphs are described such as directed, undirected, weighted graphs. Algorithms for graphs including Dijkstra's single source shortest path algorithm and algorithms for searching graphs like breadth first search and depth first search are explained. The traveling salesman problem is presented as an NP-complete problem to find the shortest route visiting all vertices of a graph once.

Uploaded by

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

GRAPHS & ALGORITHMS

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

EED, NIT Calicut

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

EED, NIT Calicut

Classification
Directed- Edge has direction or orientation
shown using arrow heads
Undirected- no orientation
Weighted- Edge is associated with a number

05/27/16

EED, NIT Calicut

V3
10

30
35

V2
12
40
V1

V4

25

50

V6

15
V5

WEIGHTED DIRECTED graph


05/27/16

EED, NIT Calicut

V3
10

30
35

V2
12
40
V1

V4

25

50

V6

15
V5

WEIGHTED UNDIRECTED graph


05/27/16

EED, NIT Calicut

V3
1

1
1

V2
1

V4

1
V1

V6

V5

NONWEIGHTED DIRECTED graph


05/27/16

EED, NIT Calicut

V3
1

1
1

V2
1

V4

1
V1

V6

V5

NONWEIGHTED UNDIRECTED graph


05/27/16

EED, NIT Calicut

Data Structure for Representation of


Graph on Computer
Adjacency List
Adjacency Matrix

05/27/16

EED, NIT Calicut

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

EED, NIT Calicut

11

V1
V2
V3

05/27/16

V4

1
2
3
4

EED, NIT Calicut

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

EED, NIT Calicut

13

A
B
C
D

05/27/16

A
0
0
1
1

B
0
0
1
0

EED, NIT Calicut

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

EED, NIT Calicut

15

Graph based Algorithms


To find shortest path
To search on the tree
To formulate or determine a spanning Tree

05/27/16

EED, NIT Calicut

16

SINGLE SOURCE SHORTEST


PATH ALGORITHM
(DIJKSTRAS algorithm)
1. Weighted Directed Graph
2. Weighted undirected graph

05/27/16

EED, NIT Calicut

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

EED, NIT Calicut

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

EED, NIT Calicut

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

EED, NIT Calicut

20

Sample graph
6 vertices
8 Edges
V3

8 weights>0

10
35

V2
12
40
V1

05/27/16

30

V4

25

50

EED, NIT Calicut

V6

15
V5
21

DIJKSTRAS Algorithm
For simplicity we consider
undirected weighted Graphs
Dijkstras algortihm uses a greedy
methods pattern

05/27/16

EED, NIT Calicut

22

Greedy Method pattern for


algorithms
We repeatedly select the best choice from
among those available in each iteration
We might be trying to optimize a cost
function over a collection of objects
We can add objects to our collections, one
at a time, always picking the next one that
optimizes the cost function, from among
those yet to be chosen
05/27/16

EED, NIT Calicut

23

Cost function for Dijkstras


Algorithm
Cost function to optimize- distance
between a node v and another node, say u,
every time we iterate
Uses the Edge Relaxation method

05/27/16

EED, NIT Calicut

24

Example
V3
10
35

V2
12
40
V1

05/27/16

30

V4

25

50

EED, NIT Calicut

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

EED, NIT Calicut

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

EED, NIT Calicut

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

EED, NIT Calicut

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

EED, NIT Calicut

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

EED, NIT Calicut

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

EED, NIT Calicut

31

Stage 1
v=v3

0
V3
10

10

30
35

V2
35

12
40

+
05/27/16

V1

V4

25

50

EED, NIT Calicut

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

EED, NIT Calicut

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

EED, NIT Calicut

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

EED, NIT Calicut

35

Search on Graph
Breadth First Search
Depth First Search

05/27/16

EED, NIT Calicut

36

Breadth First

Go to one node, say V


Visit all nodes adjacent to V
Go to next node
Visit all neighboring Nodes and proceed
Maintain a list of already visited nodes to
avoid repeated visits

05/27/16

EED, NIT Calicut

37

Depth First Search


Start from a Node
Traverse to nodes one by one to complete as
many nodes as possible
Then repeat till all nodes are covered
Maintain a list of already visited nodes to
avoid repeated visits

05/27/16

EED, NIT Calicut

38

Traveling Salesman Problem

05/27/16

EED, NIT Calicut

39

Traveling Salesman problem


The TSP is different from Dijkstras
algorithm because TSP wants to obtain the
shortest path that visits all the vertices in a
graph exactly once.

05/27/16

EED, NIT Calicut

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

EED, NIT Calicut

41

TSP

Input description: A weighted graph G.


Problem description: Find the cycle of minimum cost that visits
each of the vertices of G exactly once.
05/27/16

EED, NIT Calicut

42

Line diagram
showing Optimal
Tour through
24,978 Cities in
Sweden

05/27/16

EED, NIT Calicut

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

EED, NIT Calicut

44

P,NP, NP Complete and NP Hard


P an algorithm has polynomial time
complexity- O(nk) for n inputs and k>0
NP- algorithm has non-polynomial time
complexity
NP Complete- There a best time but we have
to conduct a large number of iterations to get
to know the best
NP Hard- No possibility of finding a best
time complexity
05/27/16

EED, NIT Calicut

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

EED, NIT Calicut

46

Deterministic Algorithm

Input

ALGORITHM

Output

Algorithm may be mostly of P class with


polynomial time complexity
05/27/16

EED, NIT Calicut

47

Randomized Algorithm
A randomized algorithm is
an algorithm that employs a degree of
randomness as part of its logic.

05/27/16

EED, NIT Calicut

48

Randomized Algorithms
Input

ALGORITHM

Output

Random Number

05/27/16

EED, NIT Calicut

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

EED, NIT Calicut

50

Randomized algorithms are not the


probabilistic analysis of expected
running time of a deterministic
algorithm

05/27/16

EED, NIT Calicut

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

EED, NIT Calicut

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

EED, NIT Calicut

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

EED, NIT Calicut

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

EED, NIT Calicut

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

EED, NIT Calicut

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

EED, NIT Calicut

57

Algorithms and Analysis in


Electrical Engineering
When we use database for Distributed Control or
such applications, we need to know the implications
of the data structures and the algorithm complexity
on the overall performance of the systems
Knowledge of the technicalities of the software
would help us to take proper decisions

One can migrate to Computer Science to


build a career by building an expertise on the
foundations of Electrical Engineering
05/27/16

EED, NIT Calicut

58

Thank You
Good Luck

05/27/16

EED, NIT Calicut

59

You might also like