Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 51

Graphs

Graphs
• A graph is a data structure that consists of a set of vertices and a
set of edges between pairs of vertices
• Edges represent paths or connections between the vertices
• The set of vertices and the set of edges must both be finite and
neither one be empty
Graphs
• A graph is a pair (V, E), where
• V is a set of vertices
• E is a set of pairs of vertices, called edges

3
Edge Types
• Directed edge
• ordered pair of vertices (u,v)
• first vertex u is the origin A B
• second vertex v is the destination

• Undirected edge A B
• unordered pair of vertices (u,v)

4
Directed and Undirected Graphs

• A graph with directed edges is called a directed graph or


digraph
• Example: flight network
• A graph with undirected edges is an undirected graph or
simply a graph

5
Weighted Graphs

• The edges in a graph may have values associated with


them known as their weights
• A graph with weighted edges is known as a weighted
graph

6
Terminology
• End vertices (or endpoints) of
an edge
• U and V are the endpoints of
a V
• Edges incident on a vertex a b
• a, d, and b are incident on V h j
• Adjacent vertices U d X Z
• U and V are adjacent
c e i
• Degree of a vertex
• X has degree 5 W g
• Parallel edges
• h and i are parallel edges f
• Self-loop
Y
• j is a self-loop

7
Terminology (cont.)

• A path is a sequence of vertices in


which each successive vertex is
adjacent to its predecessor V
• In a simple path, the vertices and a b
P1
edges are distinct except that the
U d X Z
first and last vertex may be the
same P2 h
c e
• Examples
• P1=(V,X,Z) is a simple path
W g
f
Y

8
Terminology (cont.)

• A cycle is a simple path in which


only the first and final vertices
are the same
• Simple cycle
V
a b
• cycle such that all its
vertices and edges are U d X Z
distinct C2 h
• Examples e C1
c
• C1=(V,X,Y,W,U,V) is a W g
simple cycle
• C2=(U,W,X,Y,W,V,U) is a f
Y
cycle that is not simple

9
Subgraphs
• A subgraph S of a graph G is a
graph such that
• The vertices of S are a subset of
the vertices of G
• The edges of S are a subset of
the edges of G
• A spanning subgraph of G is a Subgraph
subgraph that contains all the
vertices of G

Spanning subgraph
Graph
10
Spanning Trees

• A spanning tree of a connected


graph is a spanning subgraph that
is a tree
• A spanning tree is not unique
unless the graph is a tree
Graph

Spanning tree
11
Degree
 The degree of a vertex is the number of edges
incident to that vertex
 For directed graph,
– the in-degree of a vertex v is the number of edges
that have v as the head
– the out-degree of a vertex v is the number of edges
that have v as the tail
– if di is the degree of a vertex i in a graph G with n
vertices and e edges, the number of edges is
n 1

e( d )/ 2
0
i

12
Graph Representations

 Adjacency Lists
 Adjacency Matrix

13
Adjacency List

• An adjacency list representation of a graph uses an


array of lists
• One list for each vertex .

14
Adjacency List

1 2 5
1
2 1 5 4
5 2 3 4
4 2 3 5
4 3 5 4 1 2

• An array (Adj) of lists, one list per vertex


• For each vertex u in V,
• Adj[u] contains all edges incident on u
• Space required Θ(n+m), where n denotes the number of
vertices, and m denotes the number of edges
15
Adjacency Matrix
• Uses a two-dimensional array to represent a graph
• For an unweighted graph, the entries can be boolean values
• Or use 1 for the presence of an edge
• For a weighted graph, the matrix would contain the weights

1 2 3 4 5
1 0 1 0 0 1
1
2 1 0 0 1 1
3 0 0 0 1 0
5 2
4 0 1 1 0 1

4 5 1 1 0 1 0
3

16
Graph Traversal

1. Breadth-First Search
-----Queue
2. Depth-First Search
-----Stack
Pseudocode (BFS)

BFS (G, s) //Where G is the graph and s is the source node

1. let Q be queue.
2. Q.enqueue( s )
3. mark s as visited.
4. while ( Q is not empty)
//Removing that vertex from queue,whose neighbour will be visited now
4.1 v = Q.dequeue( )
4.2 for all neighbours w of v in Graph G //processing all the
neighbours of v
4.3 if w is not visited
4.4 Q.enqueue( w ) //Stores w in Q to further visit its
neighbour
4.5 mark w as visited.
18
19
Example (BFS)

20
BFS Graph Traversal
BFS Graph Traversal
Pseudocode (DFS)
DFS (G, s): //Where G is graph and s is source vertex

1. S.push( s ) //Inserting s in stack


2. mark s as visited.
3. while ( S is not empty):
v = S.top( )
S.pop( ) //Pop a vertex from stack to visit next
for all neighbours w of v in Graph G: //Push all the neighbours of v in
stack that are unvisited
if w is not visited :
S.push( w )
mark w as visited

23
Example (DFS)

24
25
DFS Graph Traversal
DFS Graph Traversal
Minimum Spanning Tree

• A single graph can have many different spanning trees.

• A minimum spanning tree (MST) or minimum weight


spanning tree for a weighted, connected and undirected
graph is a spanning tree with weight less than or equal to
the weight of every other spanning tree.

• The cost/weight of a spanning tree is the sum of weights


given to each edge of the spanning tree.
29
Minimum Spanning Tree

---- Kruskal’s Algorithm

---- Prim’s Algorithm


30
Prim’s Algorithm
Algorithm
1) Create a set mstSet that keeps track of vertices already included in
MST.
2) Assign a key value to all vertices in the input graph. Initialize all key
values as INFINITE. Assign key value as 0 for the first vertex so that it
is picked first.
3) While mstSet doesn’t include all vertices
….a) Pick a vertex u which is not there in mstSet and has minimum key
value.
….b) Include u to mstSet.
….c) Update key value of all adjacent vertices of u. To update the key
values, iterate through all adjacent vertices. For every adjacent vertex v,
if weight of edge u-v is less than the previous key value of v, update the
key value as weight of u-v

31
33
34
Prim’s Algorithm
Consider the graph shown below.

Which of the following edges form the MST of the given graph using Prim’s
algorithm, starting from vertex 4.
a) (4-3)(5-3)(2-3)(1-2)
b) (4-3)(3-5)(5-1)(1-2)
c) (4-3)(3-5)(5-2)(1-5) 35
d) (4-3)(3-2)(2-1)(1-5)
36
37
Minimum Spanning Tree – Prim’s Algorithm

38
Minimum Spanning Tree – Prim’s Algorithm

39
Minimum Spanning Tree – Prim’s Algorithm
(Ans)

40
Minimum Spanning Tree – Kruskal’s Algorithm

1.Sort all the edges in non-decreasing order of their weight.

2. Pick the smallest edge. Check if it forms a cycle with the


spanning tree formed so far. If cycle is not formed, include
this edge. Else, discard it.

3. Repeat step#2 until there are (V-1) edges in the spanning


tree.

41
42
43
Kruskal’s Algorithm
Consider the given graph.

What is the weight of the minimum spanning tree using the Kruskal’s
algorithm?
a) 24
b) 23
c) 15
44
d) 19
45
Minimum Spanning Tree – Kruskal’s Algorithm
Minimum Spanning Tree – Kruskal’s Algorithm

47
Single source shortest path algorithm -
Dijkstra’s Algorithm

48
50
Dijkstra’s Algorithm

You might also like