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

GRAPHS

POORNIMA.S, IT/SSN
III Sem ECE-B/2014-15

Storing Data in a Graph

Consider a situation:
You have to visit a set of cities and return back to the
original city in the end.
For this, you need to:
Find the shortest or least expensive path that starts from the
original city, visits each of the desired cities, and then
returns back to the original city.
How will you solve this problem?

2
III Sem ECE-B/2014-15

Storing Data in a Graph (Contd.)

To solve this problem, you need to:


Determine a way of representing the information,
pertaining to the various cities and the distances between
the pairs of cities.
Such pair-wise relationships can be represented in the
form of a graph.

3
III Sem ECE-B/2014-15

Defining Graph

A graph is defined as a data structure that consists of a


set of vertices (nodes) and a set of edges (arcs).
It is a collection of data items that are interconnected to
form a network.

• Graph G=(V,E)

• Vertex set V

• Edge set E pairs of vertices which are adjacent

4
III Sem ECE-B/2014-15

There are two types of graphs:


Directed graph

• •
if edges ordered pairs (u,v)
u v

Undirected graph

• •
if edges unordered pairs {u,v}
u v
A graph in which any node in a graph is adjacent to all the other
nodes in a graph is called a Complete graph.

5
III Sem ECE-B/2014-15

6
III Sem ECE-B/2014-15

7
III Sem ECE-B/2014-15

8
III Sem ECE-B/2014-15

9
III Sem ECE-B/2014-15

e1 e2 ek

Vo V1 V2 Vk-1 Vk

• Path P is a sequence of vertices v0, v1, …, vk where for


i=1,…k, vi-1 is adjacent to vi

Equivalently, P is a sequence of edges e1, …, ek where


for i = 2,…k edges ei-1, ei share a vertex

10
III Sem ECE-B/2014-15

11
III Sem ECE-B/2014-15

12
III Sem ECE-B/2014-15

13
III Sem ECE-B/2014-15

14
III Sem ECE-B/2014-15

15
III Sem ECE-B/2014-15

Representing a Graph

To implement a graph, you need to first represent the given


information in the form of a graph.
The two most commonly used ways of representing a graph
are as follows:
Adjacency Matrix – 2D matrix representation
Adjacency List - 1D array of linked list

16
III Sem ECE-B/2014-15

Adjacency Matrix
Consider the following Adjacency Matrix Representation
graph:
v1 v2 v3 v4

v1 0 1 0 0
v2 0 0 1 0
v3 0 0 0 0
v4 1 0 1 0

17
III Sem ECE-B/2014-15

Adjacency List

Consider the following Adjacency List Representation


graph:

a b a b d c
b c
c d c d
d 18
III Sem ECE-B/2014-15

19
III Sem ECE-B/2014-15

Traversing a Graph

Traversing a graph means visiting all the vertices in a graph.


You can traverse a graph with the help of the following two
methods:
Depth First Search (DFS)
Breadth First Search (BFS)

20
III Sem ECE-B/2014-15

DFS

Algorithm: DFS(v)
1. Push the starting vertex, v into the stack.
2. Repeat until the stack becomes empty:
a. Pop a vertex from the stack.
b. Visit the popped vertex.
c. Push all the unvisited vertices adjacent to the popped vertex
into the stack.

21
III Sem ECE-B/2014-15

DFS (Contd.)

Push the starting vertex, v1 into the stack

v1

22
III Sem ECE-B/2014-15

DFS (Contd.)

Pop a vertex, v1 from the stack


Visit v1
Push all unvisited vertices adjacent to v1 into the stack

v1

Visited:
v1
23
III Sem ECE-B/2014-15

DFS (Contd.)

Pop a vertex, v1 from the stack


Visit v1
Push all unvisited vertices adjacent to v1 into the stack

v2
v4

Visited:
v1
24
III Sem ECE-B/2014-15

DFS (Contd.)

Pop a vertex, v2 from the stack


Visit v2
Push all unvisited vertices adjacent to v2 into the stack

v2
v4

Visited:
v1 v2
25
III Sem ECE-B/2014-15

DFS (Contd.)

Pop a vertex, v2 from the stack


Visit v2
Push all unvisited vertices adjacent to v2 into the stack

v6
v3
v4

Visited:
v1 v2
26
III Sem ECE-B/2014-15

DFS (Contd.)

Pop a vertex, v6 from the stack


Visit v6
Push all unvisited vertices adjacent to v6 into the stack

v6
v3
v4

There are no unvisited vertices


adjacent to v6
Visited:
v1 v2 v6
27
III Sem ECE-B/2014-15

DFS (Contd.)

Pop a vertex, v3 from the stack


Visit v3
Push all unvisited vertices adjacent to v3 into the stack

v3
v4

Visited:
v1 v2 v6 v3
28
III Sem ECE-B/2014-15

DFS (Contd.)

Pop a vertex, v3 from the stack


Visit v3
Push all unvisited vertices adjacent to v3 into the stack

v5
v4

Visited:
v1 v2 v6 v3
29
III Sem ECE-B/2014-15

DFS (Contd.)

Pop a vertex, v5 from the stack


Visit v5
Push all unvisited vertices adjacent to v5 into the stack

v5
v4

There are no unvisited vertices


adjacent to v5
Visited:
v1 v2 v6 v3 v5
30
III Sem ECE-B/2014-15

DFS (Contd.)

Pop a vertex, v4 from the stack


Visit v4
Push all unvisited vertices adjacent to v4 into the stack

v4

There are no unvisited vertices


adjacent to v4
Visited:
v1 v2 v6 v3 v5 v4
31
III Sem ECE-B/2014-15

DFS (Contd.)

The stack is now empty


Therefore, traversal is complete

Visited:
v1 v2 v6 v3 v5 v4
32
III Sem ECE-B/2014-15

DFS (Contd.)

Although the preceding algorithm provides a simple and


convenient method to traverse a graph, the algorithm will
not work correctly if the graph is not connected.
In such a case, you will not be able to traverse all the
vertices from one single starting vertex.

33
III Sem ECE-B/2014-15

DFS (Contd.)

To solve this problem, you need to 1. Repeat step 2 for each


vertex, v in the graph
execute the preceding algorithm
2. If v is not visited:
repeatedly for all unvisited vertices in a. Call DFS(v)
the graph.

34
III Sem ECE-B/2014-15

BFS

Algorithm: BFS(v)
1. Visit the starting vertex, v and insert it into a queue.
2. Repeat step 3 until the queue becomes empty.
3. Delete the front vertex from the queue, visit all its unvisited
adjacent vertices, and insert them into the queue.

35
III Sem ECE-B/2014-15

BFS (Contd.)

Visit v1
Insert v1 into the queue

v1

v1
36
III Sem ECE-B/2014-15

BFS (Contd.)

Remove a vertex v1 from the queue


Visit all unvisited vertices adjacent to v1 and insert them in
the queue

v1

Visited:
v1
37
III Sem ECE-B/2014-15

BFS (Contd.)

Remove a vertex v1 from the queue


Visit all unvisited vertices adjacent to v1 and insert them in
the queue

v2 v4

v1 v2 v4
38
III Sem ECE-B/2014-15

BFS (Contd.)

Remove a vertex v2 from the queue


Visit all unvisited vertices adjacent to v2 and insert them in
the queue

v2 v4

v1 v2 v4
39
III Sem ECE-B/2014-15

BFS (Contd.)

Remove a vertex v2 from the queue


Visit all unvisited vertices adjacent to v2 and insert them in
the queue

v4 v3 v6

v1 v2 v4 v3 v6
40
III Sem ECE-B/2014-15

BFS (Contd.)

Remove a vertex v4 from the queue


Visit all unvisited vertices adjacent to v4 and insert them in
the queue

v4 v3 v6 v5

v1 v2 v4 v3 v6 v5
41
III Sem ECE-B/2014-15

BFS (Contd.)

Remove a vertex v3 from the queue


Visit all unvisited vertices adjacent to v3 and insert them in
the queue

v3 v6 v5

v3 does not have any


unvisited adjacent vertices

v1 v2 v4 v3 v6 v5
42
III Sem ECE-B/2014-15

BFS (Contd.)

Remove a vertex v6 from the queue


Visit all unvisited vertices adjacent to v6 and insert them in
the queue

v6 v5

v3 does not have any


unvisited adjacent vertices

v1 v2 v4 v3 v6 v5
43
III Sem ECE-B/2014-15

BFS (Contd.)

Remove a vertex v6 from the queue


Visit all unvisited vertices adjacent to v6 and insert them in
the queue

v5

v6 does not have any


unvisited adjacent vertices

v1 v2 v4 v3 v6 v5
44
III Sem ECE-B/2014-15

BFS (Contd.)

Remove a vertex v5 from the queue


Visit all unvisited vertices adjacent to v5 and insert them in
the queue

v5

v6 does not have any


unvisited adjacent vertices

v1 v2 v4 v3 v6 v5
45
III Sem ECE-B/2014-15

BFS (Contd.)

Remove a vertex v5 from the queue


Visit all unvisited vertices adjacent to v5 and insert them in
the queue

v5 does not have any


unvisited adjacent vertices

v1 v2 v4 v3 v6 v5
46
III Sem ECE-B/2014-15

BFS (Contd.)

The queue is now empty


Therefore, traversal is complete

v5 does not have any


unvisited adjacent vertices

v1 v2 v4 v3 v6 v5
47
III Sem ECE-B/2014-15

BFS (Contd.)

Although the preceding algorithm provides a simple and


convenient method to traverse a graph, the algorithm will
not work correctly if the graph is not connected.
In such a case, you will not be able to traverse all the
vertices from one single starting vertex.

48
III Sem ECE-B/2014-15

BFS (Contd.)

To solve this problem, you need to 1. Repeat step 2 for each


vertex, v in the graph
execute the preceding algorithm
2. If v is not visited:
repeatedly for all unvisited vertices in a. Call BFS(v)
the graph.

49
III Sem ECE-B/2014-15

Activity: Implementing a Graph by Using Adjacency Matrix Representation

Problem Statement:
You have to represent a set of cities and the distances
between them in the form of a graph. Write a program to
represent the graph in the form of an adjacency matrix.

50
III Sem ECE-B/2014-15

Applications of Graphs

Many problems can be easily solved by reducing them in


the form of a graph
Graph theory has been instrumental in analyzing and
solving problems in areas as diverse as computer network
design, urban planning, finding shortest paths and molecular
biology.

51

You might also like