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

Data Structures: Mahesh Goyani

The document discusses different ways of representing graphs in data structures. It defines key terminology used in graph theory like vertices, edges, directed/undirected graphs, weighted graphs, etc. It then describes two common ways to represent graphs in memory - using arrays and linked lists. The array representation stores the adjacency matrix of a graph, while the linked list representation links vertices together to model the graph structure.

Uploaded by

Akif Vohra
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Data Structures: Mahesh Goyani

The document discusses different ways of representing graphs in data structures. It defines key terminology used in graph theory like vertices, edges, directed/undirected graphs, weighted graphs, etc. It then describes two common ways to represent graphs in memory - using arrays and linked lists. The array representation stores the adjacency matrix of a graph, while the linked list representation links vertices together to model the graph structure.

Uploaded by

Akif Vohra
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 34

DATA

STRUCTURES

MAHESH GOYANI
MAHATMA GANDHI INSTITUTE OF TECHNICAL EDUCATION & RESEARCH CENTER
mgoyani@rediffmail.com

(C) GOYANI MAHESH 1


GRAPHS

(C) GOYANI MAHESH 2


TERMINOLOGY

 A Graph G consists of
1. Set of vertices V (Called node), (V = {v1, v2, v3, ……}) and
2. Set of Edged E (Called Arc) (i.e. E {e1, e2, e3, ……})

 A graph can be described as G = (V, E), where V is a finite and non empty set of
vertices and E is a set of pairs of edges.

 Tree must be a graph, but graph need not be a tree

A D
Set of Vertices V = {A, B, C, D, E}
Set of Edges E = {(A,B), (A, C), (A, D), (C, D), (C, E) }
C

(C) GOYANI MAHESH 3


TERMINOLOGY

 If the pair of node s that make up the arc are ordered pairs, the graph is called
directed graph (or Diagraph) {<V1, V2>}

 A graph which contains order less pairs of arc is called undirected graph { (V1, V2) }

 Vertices without an edges are called isolated vertices (Degree Zero)

 A graph is called mixed graph if it contains set of ordered and order less arcs.

v1 v2
e1
v1 v2 Isolated Vertices

Directed Graph
e1
v1 v2
e4 e3
e2 v5 e7

e5 e6
e1 v4 v3
v1 v2
e9 e8
v6
Undirected Graph
(C) GOYANI MAHESH Mixed graph 4
TERMINOLOGY
a1

a b
ISOMORPHIC
GRAPH d1

d c c1 b1

a a a b
b b
SUB e SPANING SUB
GRAPH GRAPH e
d
c d c d c

(C) GOYANI MAHESH 5


TERMINOLOGY

a d X

b c

 Two vertices are said to be adjacent if they are joined by an edge (i.e. (a, b)).
 The number of edges incident on a vertex is called degree
 Indegree of a node is number of arcs that have head as incident on that vertex.
 Outdegree of a vertex is number of arcs that have tail as incident on that vertex.
 Degree of isolated vertex is ZERO. (e.g. Node X)
 Node b has outdegree 1, indegree 2 and degree 3.
 A path from mode to it self is called cycle (Node C & D)
 If a graph contains cycle, than it is cyclic graph, other wise it is acyclic graph
 A directed acyclic graph is called a DAG

(C) GOYANI MAHESH 6


TERMINOLOGY

A Graph is said to be weighted graph (network) if every edge and / or vertices in


the graph is assigned with some weight or value.
A Weighted graph can be defined as G = (V, E, W e, Wv) where,
V = Set of vertices
E = Set of Edges
We = Set of Weight of edges
Wv = Set of Weight of Vertices

N
47 55

B 39
K

27 16
C

(C) GOYANI MAHESH 7


TERMINOLOGY

 Undirected graph is said to be Connected Graph if there exist a path from any
vertex to any other vertex. Other wise it is called Disconnected Graph.

a c a c

b d b d

a c
 Undirected graph is said to be Fully Connected
Graph (strongly connected / complete graph) if there
exist a path from all vertex to all other vertex.

 A complete graph with n vertices will have n(n-1)/2


edges. b d

(C) GOYANI MAHESH 8


e1 e2
TERMINOLOGY v1 v2 v3

e10 e3

e8 e9
v8 v9 v4

e12
e7 e11
e4

v7 v6 e5 v5
e6

 In a directed graph, a path is a sequence of edges such that the edges are
connected with each other.
 (e1, e3, e4, e5, e12, e9, e11, e6, e7, e8, e11)
 A path is said to be elementary path if it does not meet the same vertex twice.
 (e1, e3, e4, e5, e6, e7, e8)
 A path is said to be simple path if it does not meet the same edges twice.
 (e1, e3, e4, e5, e6, e7, e8, e11, e12)

 A Circuit is a path in which terminal vertex en coincides with the initial vertex e1.
 Simple circuit
 (e1, e3, e4, e5, e12, e9, e10)
 Elementary circuit
 (e1, e3, e4, e5, e6, e7, e8, e10)
(C) GOYANI MAHESH 9
GRAPH REPRESENTATION

1. Sequential representation (Using Array)


2. Linked representation (Using Linked List)

j 1 2 3 4 5
i
1 0 1 1 0 0
1 2
2 0 0 0 0 1
3 0 0 0 1 0

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

Directed Graph

(C) GOYANI MAHESH 10


ARRAY REPRESENTATION

i j 1 2 3 4 5
1 0 1 1 1 0
1 2
2 1 0 0 0 1
3 1 0 0 1 0
4 1 0 1 0 0
3 4 5
5 0 1 0 0 1

Undirected Graph

(C) GOYANI MAHESH 11


ARRAY REPRESENTATION

j 1 2 3 4 5
5 i
1 2 1 -1 5 3 -1 -1
2 7 2 -1 -1 -1 -1 7
3
0 3 -1 -1 -1 4 -1
3 4 5
4 4 2 -1 -1 -1 -1
5 -1 -1 -1 -1 0

Weighted Graph

(C) GOYANI MAHESH 12


LINKED LIST REPRESENTATION

1  2  3

1 2 2  5

3  4

3 4 5
4  1

Directed Graph 5  5

1  2 5  3 3
5
1 2
2  5 7
2 7
3
3  4 4
0
3 4 5
4 4  1 2
Weighted Graph
5  5 0

(C) GOYANI MAHESH 13


OPERATION ON GRAPH

 Create ( )
 Search ( )
 Delete ( )
 Traverse ( )

(C) GOYANI MAHESH 14


BREADTH FIRST SEARCH (BFS)

1. Input the Vertices & Edges of the Graph G =


( V, E )
2. Input the source vertex and assign it to the
variable S
3. Push S to Queue
4. While Front <= Rear, repeat step 5 & 6.
5. Pop front element and display it
6. Push the vertices, which are neighbor to just
popped element, if it is not in the queue and
displayed
7. Exit.

(C) GOYANI MAHESH 15


BREADTH FIRST SEARCH (BFS)

Front = -1
Rear = -1

Front = 0
B C

A
D E F
Rear = 0

G H
Front = 1

I A B C

Rear = 2

(C) GOYANI MAHESH 16


BREADTH FIRST SEARCH (BFS)
Front = 2

A B C D E

A Rear = 4

B C
Front = 3

D E F A B C D E F

Rear = 5

G H

Front = 4
I
A B C D E F G

Rear = 6

(C) GOYANI MAHESH 17


BREADTH FIRST SEARCH (BFS)
Front = 5

A B C D E F G
A
Rear = 6

B C
Front = 6

D E F
A B C D E F G H

Rear = 7

G H

I Front = 7

A B C D E F G H

Rear = 7

(C) GOYANI MAHESH 18


BREADTH FIRST SEARCH (BFS)
Front = 8

A B C D E F G H
A
Rear = 7

B C

D E F

G H

(C) GOYANI MAHESH 19


DEPTH FIRST SEARCH (DFS)

1. Input the Vertices & Edges of the Graph G =


( V, E )

2. Input the source vertex and assign it to the


variable S

3. Push S to STACK

4. While TOP != -1, repeat step 5 & 6.

5. Pop front element and display it

6. Push the vertices, which are neighbor to just


popped element, if it is not in the STACK and
displayed

7. Exit.

(C) GOYANI MAHESH 20


DEPTH FIRST SEARCH (DFS)

A
D TOP G TOP

B TOP E E
B C
A TOP C C C

D E F
A B D G E F H C

G H

I
E TOP F TOP H TOP

C C C C TOP

(C) GOYANI MAHESH 21


MINIMUM SPANNING TREE (MST)

 A minimum spanning tree for a graph G = (V, E) is a sub graph G1 = (V1,


E1) of G, contains all the vertices of G, and
 The vertex set V1 is same as that at graph G
 The Edge E1 is sub set of G
 And there is no cycle.

 If graph G is not connected graph than it can not have any spanning tree. In
this case, it will have spanning forest.
 Suppose if Graph G has n vertices than MST will have (n-1) edges.

 A MST for a weighted graph is a spanning tree with minimum weight.

(C) GOYANI MAHESH 22


MINIMUM SPANNING TREE (MST)

4 7

2
1 2
10 4
3 6

8 7 9 2 6
1
4
5 2

4 3
5

1
6

(C) GOYANI MAHESH 23


MINIMUM SPANNING TREE (MST)

1 2
2

9 2
8 6
1 2
5
3
4

(C) GOYANI MAHESH 24


SHORTEST PATH

 A Path from source vertex a to b is said to be shortest path if there is no other


path from a to b with lower weights.
 Dijkstar’s Algorithm is used to find shortest path.

Set V = {V1, V2, V3, …… Vn} contains the vertices and the edges E = {e1, e2, …en} of
the graph G. We is the weight of an edge e, which contains the vertices V1 and V2. Q is
a set of vertices, which are not visited. M is the vertex in Q for which weight Wm is
minimum i.e. minimum cost edge. S is source vertex.

(C) GOYANI MAHESH 25


DIJKSTRA’S ALGPRITHM

1. Input the source vertex and assign it to S.

(a). Set W(s) = 0 and

(b). Set W(v) = ______ for all verttices V is not equal to S

2. Set Q = V which is set of vertices in the graph.

3. Suppose m be a vertices in Q for which W(m) is minimum.

4. Make the vertices m as visited and delete it from Q

5. Find the vertices I which are incident with m and member of Q

6. Update the weight of vertices I = {i1, i2, ……, ik} by

(a) W(i) = min [W(i1), W(m) + W(m,i1)]

7. If any changes is made in W(v), store the vertices to corresponding


vertices I, using the array for tracing the sortest path

8. Repeat the process from strep 3 to 7 untill Q is empty

9. Exit

(C) GOYANI MAHESH 26


A Source vertex = A
6 5 V = (A, B, C, D, E, F) = Q
A

B 5 C B
V A B C D E F
C
2 3
3 F 2
W(V) 0
D
Q A B C D E F
3 1 E
E 4 D F

(C) GOYANI MAHESH 27


ITERATION : 1

A
6 5 m=A

5 W (A, A) = 0
B C
2 3 Now Q = (B, C, D, E, F)
3 F 2 Two edges incident with m
3 1
E 4 D i.e. I = (B, C)
W(B) = min( w(B), W(A) + W(A,B))
= (-, 0 + 6) = 6
W(C) = min (w(C) , W(A) + W(A,C))
= (-, 0 + 5) = 5

A
B A
V A B C D E F
C A
W(V) 0 6 5
D
Q B C D E F
E
F

(C) GOYANI MAHESH 28


ITERATION : 2

A
6 5 m = C (Because W(C) is minimum & is member of Q)

5 Now Q = (B, D, E, F)
B C
2 3 Two edges incident with m
3 F 2 i.e. I = (D, F)
3 1
E 4 D W(D) = min( w(D), W(C) + W(C,D))
= (-, 5 + 2) = 7
W(F) = min (w(F) , W(C) + W(C,F))
= (-, 5 + 3) = 8

A
B A
V A B C D E F
C A
W(V) 0 6 5 7 8
D C
Q B D E F
E
F C

(C) GOYANI MAHESH 29


ITERATION : 3

A
6 5 m = B (Because W(B) is minimum and is member of Q)

5 Now Q = (D, E, F)
B C
2 3 Three edges incident with m (C, E, F) but C is not in Q
3 F 2 So, I = (E, F)
3 1
E 4 D W(E) = min( w(E), W(B) + W(B,E))
= (-, 6 + 3) = 9
W(F) = min (w(F) , W(B) + W(B,F))
= (-, 6 + 2) = 8

A
B A
V A B C D E F
C A
W(V) 0 6 5 7 9 8
D C
Q D E F
E B
F C

(C) GOYANI MAHESH 30


ITERATION : 4

A
6 5 m = D (Because W(D) is minimum and is member of Q)

5 Now Q = (E, F)
B C
2 3 One edges incident with m
3 F 2 So, I = (F)
3 1
E 4 D
W(F) = min (w(F) , W(D) + W(D,F))
= (-, 7 + 1) = 8

A
B A
V A B C D E F
C A
W(V) 0 6 5 7 9 8
D C
Q E F
E B
F C

(C) GOYANI MAHESH 31


ITERATION : 5

A
6 5 m = F (Because W(F) is minimum and is member of Q)

5 s=F
B C
2 3 Q =(F)
3 F 2 One edges incident with m
3 1
E 4 D So, I = (E)

W(E) = min (w(F) , W(F) + W(F, E))


= (9, 9 + 3) = 9

A
B A
V A B C D E F
C A
W(V) 0 6 5 7 9 8
D C
Q E
E B
F C

(C) GOYANI MAHESH 32


A
6 5 Now E is the only chain, hence we stop the iteration and
the final table is as bellow
B 5 C A
2 3 B A
3 F 2 V A B C D E F
C A
3 1 W(V) 0 6 5 7 9 8
D C
E 4 D
E B
F C

(C) GOYANI MAHESH 33


APPLICATION

 Shortest Path (City Map)


 Flow Function (Water Flow)
 Scheduling (Cook)

(C) GOYANI MAHESH 34

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy