0% found this document useful (0 votes)
3 views

Lecture 11 - Graphs

A graph is a data structure consisting of vertices and edges that can represent relationships between elements. It can be directed or undirected, weighted or unweighted, and has various representations such as adjacency matrices and lists. Key concepts include paths, cycles, connectedness, spanning trees, and algorithms for finding minimum spanning trees like Kruskal's algorithm.

Uploaded by

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

Lecture 11 - Graphs

A graph is a data structure consisting of vertices and edges that can represent relationships between elements. It can be directed or undirected, weighted or unweighted, and has various representations such as adjacency matrices and lists. Key concepts include paths, cycles, connectedness, spanning trees, and algorithms for finding minimum spanning trees like Kruskal's algorithm.

Uploaded by

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

Graph

Graph

A graph is a data structure that describes a binary relation between elements and has a
webby looking graphical representation. Graph plays a significant role in solving a rich
class of problems.
Definition:
A graph G = (V, E) consists of a finite set of non-empty set of vertices V and set of edges
E.
V = { v1, v2, …, vn}
E = {e1, e2, …en}

Each edge e is a pair (v, w) where v, w є V. The edge is also called arc.
Eg, of graphs:

1 1 1
e1 e2
e1 e2 e1
e6
2 3 2 e3
2 e5 3 e3 e4 e5 e6
e2
e4 e3
4 5 6 7 3
4

G1 G2 G3
V(G1) = {1, 2, 3, 4} V(G3) = {1, 2, 3}
E(G1) = {e1, e2, e3, e4, e5, e6} E(G3) = {e1, e2, e3}
e1 = (1, 2) e1 = (1, 2)
e2 = (1, 3) e2 = (2, 3)
e3 = (3, 4) e3 = (1, 3)
e4 = (2, 4)
e5 = (2, 3)
e6 = (1, 4) Now, specify the vertices
and edges for G2.

1
Graph

The vertices are represented by points or circles and the edges are line segments
connecting the vertices. If the graph is directed, then the line segments have arrow heads
indicating the direction.

Directed Graphs:
If every edge (i, j), in E(G) of a graph G is marked by a direction from i to j, then the
graph is called directed graph. It is often called diagraph.

1
e1 e2
e6
2 e5 3

e4 e3

4
G4
In edge e = (i, j), we say, e leaves i and enters j. In diagraphs, self loops are allowed. The
indegree of a vertex v is the number of edge entering v. The outdegree of a vertex v is the
number of edges leaving v. The sum of the indegrees of all the vertices in a graph equals
to the sum of outdegree of all the vertices.

Undirected Graph:
If the directions are not marked for any edge, the graph is called undirected graph. The
graphs G1, G2, G3 are undirected graphs. In an undirected graph, we say that an edge e =
(u, v) is incident on u and v (u and v are connected).
Undirected graph don’t have self loops. Incidence is a symmetric relation i.e. if e = (u, v)
Then u is a neighbor of v and vice versa. The degree of a vertex, d(v), is the total number
of edges incident on it.

2
Graph

The sum of the degrees of all the vertices in a graph equals twice the number of edges if
d(v) = 0, v is isolated. If d(v) = 1, v is pendent.

Weighted Graphs:
A graph is said to be weighted graph if every edge in the graph is assigned some weight
or value. The weight is a positive value that may represent the cost of moving along the
edge, distance between the vertices etc.
The two vertices with no edge (path) between them can be thought of having an edge
(path) with weight infinite.

1 2 2

6 1
3

3 5 4

Additional Terminology in Graph

Adjacent and Incident


Two vertices i and j are called adjacent if there is an edge between the two. Eg. The
vertices adjacent to vertex A, in the fig below are B, C, D. The adjacent vertices of C are
A and D.
e1
A B
e2
e3
e4
C D
G6

3
Graph

If e(i, j) is an edge on E(G), then we say that the edge e(i, j) is incident on vertices i and j.
Eg. in the above figure, e4 is incident on C and D. If (i, j) is directed edge, then i is
adjacent to j and j is adjacent from i.

Path:
A path is a sequence or distinct vertices, each adjacent to the next. For e.g. the sequence
of vertices e1, e3, e4 (i.e (B,A), (A,C), (C,D)) of the above graph form a path from B to D.
The length of the path is the number of edges in the path. So, the path from B to D has
length equal to three.
In a weighted graph, the cost of a path is the sum of the costs of its edges. Loops have
path length of 1.

Cycle:
A cycle is a path containing at least three vertices such that the last vertex on the path is
adjacent to the first. In the above graph G6, A, C, D, A is a cycle.
Connected:
Any graph is connected provided there exists a path (directed or undirected) between any
two nodes. A digraph is said to be strongly connected if, for any two vertices there is a
direct path from i to j.
A digraph is said to be weakly connected if, for any two vertices i and j, there is a direct
path from i to j or j to i. or A weakly-connected graph is a directed graph for which its
underlying undirected graph is connected.
Eg.

A D E E

A D A D

B C
B C B C

Connecte Strongly Weakly


d Connected Connected

Complete graph
A complete graph is a graph in which there is an edge between every pair of vertices.

4
Graph

Trees and Graph


A tree is a connected graph with no cycle. A tree has |E| = |V| - 1 edges. Since, it is
connected, there is a path between any two vertices.

Representation of Graphs
A graph can be represented in many ways. Some of them are described in this section.

Adjancency matrix
Adjancency matrix A for a graph G = (V, E) with n vertices, is n x n matrix, such that
Aij = 1, if there is an edge from vi to vj
Aij = 0, if there is no such edge

We can also write,


1 if and only if (vi, vj) exists
A(i, j) =

0 otherwise
Eg; An adjacency matrix for the following undirected graph is

V1

V2 V3 V4

V5
A=
V1 V2 V3 V4 V5
V1 0 1 1 1 0
V2 1 0 1 0 1
V3 1 1 0 1 1
V4 1 0 1 0 1
V5 0 1 1 1 0

5
Graph

If the graph is directed, then the adjacency matrix will be as follows. The number in each
row tells the outdegree of that vertex.

V1

V2 V3 V4

V5

A=

V1 V2 V3 V4 V5
V1 0 1 1 1 0
V2 0 0 1 0 1
V3 0 0 0 1 0
V4 0 0 0 0 1
V5 0 0 1 0 0

In this representation we require n2 bits to represent a graph with n nodes. It is a simple


way to represent a graph, but it has following disadvantages:
- it takes O(n2) space
- it takes O(n2) time to solve most of the problems.

Adjacency List Representation


The n rows of an adjacency matrix can be represented as n linked lists. This is one list for
each node in a graph. Each list will contain adjacent nodes. Each node has two fields,
Vertex and Link. The Vertex field of a node p will contain the nodes that are adjacent to
the node p.

6
Graph

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

Graph Traversals
A graph traversal means visiting all the nodes of the graph. Basically, there are two
methods of graph traversals.
1. Breadth First Traversal
2. Depth First Traversal
1. Breadth First Traversal
The Breadth First Traversal begins with a given vertex and then it next visits all the
vertices adjacent to v, putting the vertices adjacent to these in a waiting list to be
traversed after all vertices adjacent to v have visited. Breadth First Traversal uses
queue.

4 8 A B C

2 5 7 D E F

1 3 6 G H I

Starting from 1 Breadth First Starting from A Breadth First


Traversal yields 1, 2, 3, 4, 5, 6, 8, 7 Traversal yields A, B, D, E, C, G, F,
H, I

7
Graph

Algorithm:
void BFTraversal(Graph G)
{
for each vertex v in G
visited[v] = false;
for each vertex v in G
if(visited[v]==false)
{
enqueue(V, Q);
do
{
v=dequeue(q);
visited[v]=true;
visited(v);
for each vertex w adjacent to v
if(visited[w]==false)
enqueue(w, q);
}while(isempty(Q)==false)
}
}

2. Depth First Traversal


The Depth First Traversal algorithm is roughly analogous to preorder tree traversal,
the algorithm follows:
suppose that the traversal has just visited a vertex v, and let w0, w1, …wk be the
vertices adjacent to v
- next, visit w0 and keep w1, …wk waiting
- after visiting w0, we traverse all the vertices to which it is adjacent before
returning to traverse w1, …wk.
Depth first traversal uses stack to store the adjacent vertices but one.
For above 2 figures Depth First Traversal yields

8
Graph

Adjacency list Adjacency list


1 2, 3 A B, D, E
2 1, 4, 5 B A, C, E
3 1, 6 C B, F
4 2, 8 D A, G
5 2, 7 E A, B, G
6 3, 7 F C
7 5, 6, 8 G D, E, H
8 4, 7 H G, I
I H
DF Traversal: DF Traversal:
1, 2, 4, 8, 7, 5, 6, 3 A, B, C, F, E, G, D, H, I

Algorithm:
void DFTraversal(Graph G)
{

for each vertex v in G


Visited[v]=false;

for each vertex v in G


If(visited[v]==false)
Traverse(v);
}
void Traverse(Vertex v)
{
Visited[v]=true;
Visited(v);
For each vertex w adjacent to v
{
If(visited[w]==false)
Traverse(w);
}
}

Subgraph:
Let H be a graph with vertex set v(H) and edge set E(H) and, similarly,
let G be a graph with vertex set V(G) and edge set E(G). Then H is said

9
Graph

to be a subgraph of G, written as H ≤ G if V(H) ≤ V(G) and E(H) ≤


E(G).

C
C

A B A B A B
(i) (ii) (iii)

Here, second and third graph are subgraph of the


first.

Spanning Tree:
Let a graph G=(V,E) be a graph. If T is a subgraph of G and contains all the vertices but
no cycles/circuits, then ‘T’ is said to be spanning tree of G.
In other words, the spanning tree of an undirected graph G is the free tree formed from
graph edges which connects all the vertices of G.

v1 v5

v4
v2 v7

v3 v6

Undirected Graph G Spanning Tree of G

For a given graph G=(V, E), if Breadth-First search and Depth-First search call is made,
then the resultant tree that is generated is the spanning tree of the graph G. Thus, by
making a Depth-First search on G, we get the Depth-First spanning tree. Similarly, on
applying Breadth-First search on graph ‘G’ we get Breadth First spanning tree.

10
Graph

v1 v1
v1

v2 v2 v3 v4
v3 v4
v2 v3 v4

v5 v5
v5
Graph G BS Spanning Tree and DF Spanning Tree of G

Minimum Spanning Tree


The cost of a spanning tree of a weighted, undirected graph is the sum of the costs
(weights) of the edges in tree.
The minimum spanning tree of a weighted undirected graph is the spanning tree that
connects all the vertices in G at lowest cost. So, if T is the minimum spanning tree of
graph G and T’ is any other spanning tree of G then,
W(T) ≤ W(T’)

2 2 2
A B A B A B A B
2
3 3 4 4 3 3
4
3
C D C D C
1
D C D
1 1
G T1 T2 T3
2 2
A B A B A B
2 3 4 2 3

C D C D C D
1 1

T4 T5 T6

Here, T1 to T6 are the spanning trees of G. Among them, T4 has minimum cost (i. e. 5).
So, T4 is the minimum spanning tree of graph G
11
Graph

There are several algorithms available to determine the minimal spanning tree of a given
weighted graph.

12
Graph

Kruskal’s Algorithm
To determine minimum spanning tree consider a graph G with n vertices.

Step 1: List all the edges of the graph G with increasing weights
Step 2: Proceed sequentially to select one edge at a time joining n vertices of G such that
no cycle is formed until n-1 edges are selected.
Step 3: Draw the n-1 edges that were selected forming a minimal spanning tree T of G.

2
A B
4 1 1
3 0
2 7
C D E
4
5 3
6
F 1 G

We have,
Edges: AD FG AB CD BD AC DG CF EG DE DF BE

Weights 1 1 2 2 3 4 4 5 6 7 8 10

A A
B B
1 1

C D E C D E

F G F G
1

(1) (2)

13
Graph

A B A B
2 2
1 1

C D E C D E
2

F G F G
1 1
(3) (4)

A B A B
2 2
1 1

C D E C D E
2 2
4 4 4

F G F G
1 1

(5) (6)
Hence, the minimum spanning tree is generated.
Round Robin Algorithm
This method provides better performance when the number of edges is low. Initially each
node is considered to be a partial tree. Each partial tree is maintained in a queue Q.
Priority queue is associated with each partial tree, which contains all the arcs ordered by
their weights.
The algorithm proceeds by removing a partial tree, T1, from the front of Q, finding the
minimum weight arc a in T1; deleting from Q, the tree T2, at the other end of arc a;
combining T1 and T2 into a single new tree T3 and at the same time combining priority
queues of T1 and T2 and adding T3 at the rear of priority queue. This continues until Q
contains a single tree, the minimum spanning tree.

14
Graph

Q Priority queue
{A} 1, 2
{B} 2, 2, 3, 3
{C} 1, 3, 4
{D} 2, 4, 5
{E} 3, 5

A 3
2 E

1 B
2 5
3

C D
4

A
E

1 B

C D

Q Priority queue
{B} 2, 2, 3, 3
{D} 2, 4, 5
{E} 3, 5
{A, C} 2, 3, 4

15
Graph

A
E

1 B

C D

Q Priority queue
{E} 3, 5
{A, C} 2, 3, 4
{B, D} 2, 3, 3, 4, 5

A
3 E

1 B

C D

Q Priority queue
{A, C} 2, 3, 4
{E, B, D} 2, 3, 4, 5, 5

Q Priority queue
{A, C, E, B, D} 3, 3, 4, 4, 5, 5

16
Graph

A 2
3 E

1 B
2

C D

Only 1 partial tree is left in the queue, which is the required minimum spanning tree.

Shortest Path Algorithm


There are many problems that can be modeled using graph with weight assigned to their
edges. Eg, we may set up the basic graph model by representing cities by vertices and
flights by edges. Problems involving distances can be modeled by assigning distances
between cities to the edges. Problems involving flight time can be modeled by assigning
flight times to edges. Problem involving fares can be modeled by assigning fares to the
edges.
Thus, we can model airplane or other mass transit routes by graphs and use shortest path
algorithm to compute the best route between two points.
Similarly, if the vertices represent computers; the edges represent a link between
computers; and the costs represent communication costs, delay costs, then we can use the
shortest-path algorithm to find the cheapest way to send electronic news, data from one
computer to a set of other computers.
Basically, there are three types of shortest path problems:
Single path: Given two vertices, s and d, find the shortest path from s to d and its length
(weights).
Single source: Given a vertex, s find the shortest path to all other vertices.

All pairs: Find the shortest path from all pair of vertices.

17
Graph

Let source vertex be a. We will find the shortest path to all the other vertices.
3
b c
3
1
3 z
a
2
2
d 3 e

Vertex Cost Path


b 1 a, b
c 4 a, b, c
d 2 a, d
e 4 a, b, e
z 6 a, b, e, z

Dijkstra’s Algorithm to find the shortest path

1. Mark all the vertices as unknown


2. for each vertex v keep a distance dv from source vertex s to v initially set to infinity
except for s which is set to ds = 0
3. repeat these steps until all vertices are known
i. select a vertex v, which has the smallest dv among all the unknown vertices
ii. mark v as known
iii. for each vertex w adjacent to v
i. if w is unknown and dv + cost(v, w) < dw
update dw to dv + cost(v, w)

5
b d ∞ 5 ∞
6 b d
4 6 ∞
4
1 z
a 8 2 1
8 2 z
0 a
3
2 3
c e 2
1
c 1 e
0
∞ 0 ∞ 18
Given graph with weights (a)
4(a) 5 Graph
b d ∞ 3(a,c 5 10(a,c)
6 ) b d
4 6 ∞
∞ 4
1 z
0 a 8 2 1 z
0 a 8 2
3
2 3
c e 2
1
∞ c 1 e
2(a) 0
0 12(a,c)
(b) (c)

3(a,c 5 8(a,c,b 3(a,c 5 8(a,c,b


) b d ) ) b d )
6 6
4 ∞ 4 ∞
1 z 1 z
0 a 8 2 a 8 2
0
3 3
2 2
c 1 e c 1 e
2(a) 0 12(a,c) 2(a) 0 10(a,c,b,d)
(d) (e)

3(a,c 5 8(a,c,b
) b d )
6
4
1 z
a 8 2
13(a,c,b,d,e
3)
2
c 1 e
2(a) 0 10(a,c,b,d)
(f)

19
Graph

Greedy Algorithm
One of the simplest approaches that often leads to a solution of an optimization problem.
This approach selects the best choice at each step, instead of considering all sequences of
steps that may lead to an optimal solution. Algorithm that make what seems to be the
“best” choice at each step are called greedy algorithm.

Transitive Closure
In all shortest path, we noticed that every time we have to determine whether there is a
path from vertex i to j. This can be accomplished by observing a matrix, say d(n). The path
exists if and only if the entry does not have the value infinity. In matrix d(n), we consider
only path (not the weight). Thus, (dk) can be regarded as Boolean matrix.
If there is a path from i to j, then (i, j)th entry in dk is 1 (which means true) otherwise the
value is 0. If ‘A’ is the adjacency matrix of graph ‘G’, then the transitive closure is
defined to be a Boolean matrix with the above property.

1 2

3 4

Directed
Graph
A+ Matrix has the following property:
1 if there is a path from i to j
0 otherwise
The matrix A+ is called the transitive closure. The Transitive closure helps to determine
which pair or vertices in a digraph are connected by a path. It takes O(n4) time
complexity.

Let’s consider the following directed graph

20
Graph

B
C

A E
D
Now, let adj2 is a matrix that stores information about the existence of path of length 2.
The adj2 matrix (path matrix of length 2) can be represented as:
A B C D E
A 0 0 0 1 1
B 0 0 0 1 1
C 0 0 0 1 1
D 0 0 0 1 0
E 0 0 0 0 1

Similarly, adj3 can be represented as:

A B C D E
A 0 0 0 1 1
B 0 0 0 1 1
C 0 0 0 1 1
D 0 0 0 0 1
E 0 0 0 1 0

And adj 4 as:


A B C D E
A 0 0 0 1 1
B 0 0 0 1 1
C 0 0 0 1 1
D 0 0 0 1 0
E 0 0 0 0 1

21
Graph

To calculate the path matrix, we just multiply the matrix itself. For eg, to calculate path
matrix 3, the matrix of adj1 is multiplied 3 times itself. While multiplying, the addition
operatin is replaced by ‘||’ operation and multiplication is replaced by ‘&&’ operation.

To find at least one path between two nodes, we can check adj1, adj2, adj3, …,adjmaxnodes-
1. If any of the adjacency matrix give true value for node ‘i’ and ‘j’, then there exist at
least a path between nodes ‘i’ and ‘j’. To store the adjacency values for path, we use a
path matrix as:
A B C D E
A 0 0 0 1 1
B 0 0 0 1 1
C 0 0 0 1 1
D 0 0 0 1 1
E 0 0 0 1 1

The length of path can be 1, 2, 3 or 4… and there is no termination of the order of the
length. Let ‘m’ is the highest order of length and ‘n’ is the no. of total nodes. If the graph
supports m>n, then at least one of the node is visited twice or more times in the path. It
means, we can discard the cycling path from the node to the same node. The removal
process is repeated until no node is repeated in the path. This ensures that the paths that
exist in the graph be less than or equal to n. The final matrix path in which no path exists
that has length greater than ‘n’ is known as transitive closure of the matrix adj.

Warshall’s Algorithm
Transitive closure method of finding path between nodes is quite inefficient because it
processes the adjacency matrix lots of times. An Efficient method for calculating
transitive closure is by Warshall’s algorithm whose time complexity is only O(n3).

a b

d c

We first find the matrices d0, d1, d2, d3, d4, and d4 is the transitive closure.

22
Graph

d1 has 1 as its (i,j)th entry if there is a path from vi to vj that has v1 = a as an interior
vertex.

d2 has 1 as its (i,j)th entry if there is a path from vi to vj that has v1 and v2 as interior
vertex. d3 has 1 as its (i,j)th entry if there is a path fro vi to vj that has v1 v2 and v3 as
interior vertex.

Finally, d4 has 1 as its (i,j)th entry if there is a path from vi


to vj that has v1, v2, v3 and v4 as interior vertex.

The matrix d4 is the transitive closure.

23
Graph

Warshall’s Algorithm
procedure warshall (MR : n x n zero one matrix)
{
D = MR
for k = 1 to n
begin
for i = 1 to n
begin
for j = 1 to n
dij = dij (dik dkj)
end
}
D = [dij] is the transitive closure.

Topological Sorting
A topological sort is an ordering of vertices in a directed acyclic (graph with no cycle)
graph (dag), such that if there is a path from vi to vj, then vi appears before vj in the
ordering. A topological sort of a graph can be viewed as an ordering of its vertices along
a horizontal line so that all directed edges go from left to right.

Applications:
Consider the course available at university as the vertices of a directed graph; where there
is an edge from one course to another if the first is a prerequisite for the second.

A topological ordering is then a listing of all the courses such that all prerequisite for a
course appear before it does.

24
Graph

A B

C D E

F G

G1

0 1 2 3 4

5 6 7 8 9
G2
Fig: Directed Graph with no Directed
Cycle

Algorithms to find out topological ordering


1. Breadth First Ordering
2. Depth First Ordering

1. Breadth First Ordering


In Breadth First Topological ordering of a directed graph with no cycles, we start by
finding the vertices that should be first in the topological order and then apply the fact
that every vertex must come before its successors in the topological order. The vertices
that come first are that are not successors of any other vertex.

Determining the Breadth First Topological ordering:


1. Fill the information from the graph into the table( Vertices, Predecessors and
Predecessor count)
2. Visit the vertex that have Predecessor count zero (0).
3. Decrement the Predecessor count value by 1 where ever the visited vertex
appears in the Adjacent Predecessor vertices column.
4. Repeat steps 2 and 3 until all the vertices has been visited.

25
Graph

From the graph G1 we get the following information:

Vertex Adjacent predecessor vertices Predecessor count


A - 0
B A 10
C A, D 210
D A, B, E 3210
E B 10
F C, D, G 3210
G D, E 210

The breadth first topological ordering gives the following

A B E D C G F

From the graph G2 we get following information:


Vertex Adjacent predecessor vertices Predecessor count
0 6 10
1 0, 6 210
2 3,6 210
3 - 0
4 3, 9 210
5 0 10
6 - 0
7 1, 3, 8 3210
8 3, 4 210
9 - 0

The breadth first topological ordering gives the following:


3 6 9 0 2 4 1 5 8 7

26
Graph

2. Depth First Ordering:


Start by finding a vertex that has no successors and place it last in the order.
After, we have, by recursion placed all the successors of a vertex into the topological
order, then place the vertex itself in the order (before any of its successors).

3 6 9 0 2 4 1 5 8 7

Fig: Breadth First Topological


Sort

9 6 3 4 8 2 0 5 1 7

Fig: Depth First Topological Sort

27
Graph

Determining the Depth First Topological ordering:


1. Fill the information from the graph into the table( Vertices, Successors and
Successor count)
2. Push all the vertices into stack and visit the vertex that is on the top.
3. Decrement the Successor count value by 1 where ever the visited vertex appears
in the Adjacent Successor vertices column.
4. Repeat steps 2 and 3 until all the vertices has been visited.

From the graph G1 we get the following information:


Vertex Adjacent successor vertices Successor count
A B, C, D 3210
B D, E 210
C F 10
D C,F, G 3210
E D, G 210
F - 0
G F 10

The depth first topological ordering gives the following


A B E D C G F

From the graph G2 we get the following information:


Vertex Adjacent successor vertices Predecessor count
0 1, 5 210
1 7 10
2 - 0
3 2, 4, 7, 8 43210
4 8 10
5 - 0
6 0, 1, 2 3210
7 - 0
8 7 10
9 4 10

The depth first topological ordering gives the following:


9 6 3 4 8 2 0 5 1 7

28

You might also like