You are on page 1of 23

GRAPH

GRAPHS : DEFINITIONS

A graph consists of a set of nodes(or vertices)


and a set of arcs(or edges). Each arc in a
graph is specified by a pair of nodes. {(A,B),
(C,D)}
A Graph G consists of two sets V and E. V is a
finite non-empty set of vertices and E is a set
of pairs of vertices called edges.
G=(V,E)

Graphs

9/22/16

Graphs

9/22/16

DEFINITIONS:

In an undirected graph, an edge is unordered


ie (v1,v2) = (v2,v1)
In a directed graph, an edge is ordered
<v1,v2> != <v2,v1>. Here first element of
the pair, v1 is called start vertex and second
element of the pair v2 is called the end
vertex.
directed graph, the max number of edges is
n(n-1).

Graphs

9/22/16

Graphs

9/22/16

Weighted graphs

Edges

or vertices can have


associated weights
Weights can represent anything

Distances, costs, capacities, etc.

2
4

5
b
2

c
1

9/22/16

d
4

Graphs

DEFINITIONS :

If there is more than one edge between two


vertices, it is called a multigraph.
2

Graphs

9/22/16

COMPLETE GRAPH.
An undirected graph, in which every vertex has
an edge to all other vertices is called complete
graph. Maximum number of edges in complete
graph is n(n-1)/2. In a directed graph, the max
number of edges is n(n-1).

Graphs

9/22/16

CONNECTED GRAPH:

An undirected graph is said to be connected


if there exist a path between every pair of
vertices vi and vj.
A

Graphs

9/22/16

SUBGRAPH:

A subgraph of G is a graph G1 such that V(G1)


is a subset of v(G) and E(G1) is a subset of
E(G).
A

C
SUBGRAPHS

Graphs

9/22/16

10

SPANNING TREE

A spanning tree of a graph G is a Subgraph that


includes all vertices of the original graph and
no cycle.
If graph G is not connected there is no spanning tree.

If original graph has n vertices, the spanning tree has n


vertices.
A graph may have multiple spanning trees.

PPT

SPANNING TREE

2
4

Graphs

9/22/16

12

MINIMUM COST SPANNING TREE


Reduce graph to tree of smallest cost
1

2
a

d
5

10

h
g

3
f

Tree cost is sum of edge weights/costs.

MINIMUM COST SPANNING TREE


Approach Remove the most expensive
edge on each cycle
1
b

d
5

10

Spanning tree cost = 29.

h
g

3
f

MINIMUM COST SPANNING


TREES:

Prims Algorithm
Kruskals Algorithm

Graphs

9/22/16

15

PRIMS ALGORITHM

This method builds minimum spanning tree edge by edge


Select an edge from the graph whose cost is minimum
among all the edges.
Add the next edge (i,j) to the tree such that vertex i is
already in the tree & vertex j is new one & cost of the
edge (i,j) is minimum among all the edges (k.l) such that
k is in the tree & l is not in the tree.
While including any edge ensure that it doesnt form a
cycle.

Graphs

9/22/16

16

PRIMS
ALGORITHM
Find out
minimum cost & spanning tree for
following graph by applying Prims algorithm

1000
Bombay
Lonavala
4000
Pune

Graphs

2500

9/22/16

17

PRIMS
ALGORITHM
Spanning
tree using Prims algorithm is
1000
Bombay
Lonavala

Pune

2500

Total weight = 3500 This is also minimal spanning


tree

Graphs

9/22/16

18

2
6

5
6

Graphs

9/22/16

19

0
1

1
0

--

--

Graphs

9/22/16

Visited

distance

from

20

SOLVE USING PRIMS ALGO.


12

A
17

15

F
10

19
E

14

Graphs

9/22/16

2
6

21

KRUSKALS ALGORITHM:

I n this method edges are added to the spanning tree in


increasing order of cost.
If edge E forms a cycle in the spanning tree it is
discarded.

Graphs

9/22/16

22

CONSTRUCT MINIMUM SPANNING


TREE USING KRUSKALS ALGORITHM:
12

A
17

15

F
10

19
E

14

Graphs

9/22/16

2
6

23

You might also like