09_DS_MCA_Graphs.pdf

Prasanna David
Prasanna DavidAssistant Professor at PG Centre, SBRR Mahajana College (Pooja Bagavat Memorial Mahajana Education Centre)
Data Structures
Module – 5 (part 1)
Graphs:Definitions, Terminologies, Matrix and Adjacency List
Representation Of Graphs, Elementary Graph
operations, Traversal methods: Breadth First Search and Depth First
Search.
Insertion Sort, Radix sort, Address
Calculation Sort. Hash Table organizations, Hashing Functions, Static
and Dynamic Hashing
Adopted/Modified for :
Data Structures – MCA I Semester
Vidya Vikas Institute of Engineering and Technology
Mysore
2022-23
Introduction
Graphs
Graphs
A graph is a non-linear data structure consisting of a
set of vertices/nodes and a set of edges that connect
these vertices.
Graphs provide a powerful tool for modeling and
solving problems in various domains, including
computer networking, social networks, transportation
systems, and data analysis.

Graphs - Definition
• A graph G = (V, E), where
• V is the vertex set.
• E is the edge set.
– Vertices are also called nodes and points.
– Each edge connects two different vertices.
– Edges are also called arcs and lines.
u v
CHAPTER 6 5
Definition
 A graph G consists of two sets
– a finite, nonempty set of vertices V(G)
– a finite, possible empty set of edges E(G)
– G(V,E) represents a graph
 An undirected graph is one in which the pair of vertices in a edge is
unordered, (v0, v1) = (v1,v0)
 A directed graph is one in which each edge is a directed pair of
vertices, <v0, v1> != <v1,v0>
tail head
CHAPTER 6 6
Examples for Graph
0
1 2
3
0
1
2
0
1 2
3 4 5 6
G1
G2
G3
V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)}
V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)}
V(G3)={0,1,2} E(G3)={<0,1>,<1,0>,<1,2>}
complete undirected graph: n(n-1)/2 edges
complete directed graph: n(n-1) edges
complete graph incomplete graph
CHAPTER 6 7
Complete Graph
 A complete graph is a graph that has the
maximum number of edges
– for undirected graph with n vertices, the maximum number of
edges is n(n-1)/2
– for directed graph with n vertices, the maximum
number of edges is n(n-1)
– example: G1 is a complete graph
CHAPTER 6 8
Adjacent and Incident
 If (v0, v1) is an edge in an undirected graph,
– v0 and v1 are adjacent
– The edge (v0, v1) is incident on vertices v0 and v1
 If <v0, v1> is an edge in a directed graph
– v0 is adjacent to v1, and v1 is adjacent from v0
– The edge <v0, v1> is incident on v0 and v1
Undirected Graph
2
3
8
10
1
4
5 9 11
6
7
Directed Graph (Digraph)
2
3
8
10
1
4
5 9 11
6
7
Complete Undirected Graph
Has all possible edges.
n = 1 n = 2 n = 3 n = 4
CHAPTER 6 12
0 2
1
(a)
2
1
0
3
(b)
self edge multigraph:
multiple occurrences
of the same edge
Figure 6.3
 Restrictions on Graph
CHAPTER 6 13
 A subgraph of G is a graph G’ such that V(G’)
is a subset of V(G) and E(G’) is a subset of E(G)
 A path from vertex vp to vertex vq in a graph G,
is a sequence of vertices, vp, vi1, vi2, ..., vin, vq,
such that (vp, vi1), (vi1, vi2), ..., (vin, vq) are edges
in an undirected graph
 The length of a path is the number of edges on it
Subgraph and Path
CHAPTER 6 14
0 0
1 2 3
1 2 0
1 2
3
(i) (ii) (iii) (iv)
(a) Some of the subgraph of G1
0 0
1
0
1
2
0
1
2
(i) (ii) (iii) (iv)
(b) Some of the subgraph of G3
0
1 2
3
G1
0
1
2
G3
CHAPTER 6 15
 A simple path is a path in which all vertices, except possibly the first
and the last, are distinct
 A cycle is a simple path in which the first and the last vertices are the
same
 In an undirected graph G, two vertices, v0 and v1, are connected if
there is a path in G from v0 to v1
 An undirected graph is connected if, for every pair of distinct vertices
vi, vj, there is a path from vi to vj
Simple Path and Cycle
CHAPTER 6 16
0
1 2
3
0
1 2
3 4 5 6
G1
G2
connected
tree (acyclic graph)
Path Finding
Path between 1 and 8.
2
3
8
10
1
4
5 9 11
6
7
4
8
6
6
7
5
2
4
4 5
3
Path length is 20.
Another Path Between 1 and 8
2
3
8
10
1
4
5 9 11
6
7
4
8
6
6
7
5
2
4
4 5
3
Path length is 28.
Example Of No Path
No path between 2 and 9.

2 
3

8

1
0

1

4 
5 
9 
11

6 
7
Example Of Not Connected

2 
3

8

1
0

1

4 
5 
9 
11

6 
7
Connected Graph Example

2 
3

8

1
0

1

4 
5 
9 
11

6 
7
CHAPTER 6 22
 A connected component of an undirected graph is a maximal connected
subgraph.

A maximal subgraph is a subgraph within a larger graph that cannot be
extended by adding any more vertices or edges while maintaining the
subgraph's properties.

 A tree is a graph that is connected and acyclic.

Connected Component
Connected Components

2 
3

8

1
0

1

4 
5 
9 
11

6 
7
Not A Component

2 
3

8

1
0

1

4 
5 
9 
11

6 
7
CHAPTER 6 25
A graph with two connected components
1
0
2
3
4
5
6
7
H1
H2
G4 (not connected)
connected component (maximal connected subgraph)
CHAPTER 6 26
 A directed graph is strongly connected if
there is a directed path from vi to vj and also
from vj to vi.
 A strongly connected component is a
maximal subgraph that is strongly connected.
Connected Component
CHAPTER 6 27
Strongly connected components of G3
0
1
2
0
1
2
G3
not strongly connected
strongly connected component
(maximal strongly connected subgraph)
CHAPTER 6 28
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
e di
n



( ) /
0
1
2
CHAPTER 6 29
undirected graph
degree
0
1 2
3 4 5 6
G1 G2
3
2
3 3
1 1 1 1
directed graph
in-degree
out-degree
0
1
2
G3
in:1, out: 1
in: 1, out: 2
in: 1, out: 0
0
1 2
3
3
3
3
CHAPTER 6 30
ADT for Graph
structure Graph is
objects: a nonempty set of vertices and a set of undirected edges, where each
edge is a pair of vertices
functions: for all graph ∈ Graph, v, v1 and v2 ∈ Vertices
Graph Create()::=return an empty graph
Graph InsertVertex(graph, v)::= return a graph with v inserted. v has no
incident edge.
Graph InsertEdge(graph, v1,v2)::= return a graph with new edge
between v1 and v2
Graph DeleteVertex(graph, v)::= return a graph in which v and all edges
incident to it are removed
Graph DeleteEdge(graph, v1, v2)::=return a graph in which the edge (v1, v2)
is removed
Boolean IsEmpty(graph)::= if (graph==empty graph) return TRUE
else return FALSE
List Adjacent(graph,v)::= return a list of all vertices that are adjacent to v
Graphs
 Adjacency Matrix
 Adjacency Lists
Graph Representations
CHAPTER 6 32
1. Adjacency Matrix
 Let G=(V,E) be a graph with n vertices.
 The adjacency matrix of G is a two-dimensional n by n array,
say adj_mat

If the edge (vi, vj) is in E(G), adj_mat[i][j]=1

If there is no such edge in E(G), adj_mat[i][j]=0
 The adjacency matrix for an undirected graph is symmetric;
 The adjacency matrix for a digraph need not be symmetric
CHAPTER 6 33
Examples for Adjacency Matrix
[
0
1
1
1
1
0
1
1
1
1
0
1
1
1
1
0
] [
0
1
0
1
0
0
0
1
0]
[
0
1
1
0
0
0
0
0
1
0
0
1
0
0
0
0
1
0
0
1
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
]
G1
G2
G4
0
1 2
3
0
1
2
1
0
2
3
4
5
6
7
symmetric
undirected: n2
/2
directed: n2
CHAPTER 6 34
Merits of Adjacency Matrix
 From the adjacency matrix, it is easy to determine the
connection of vertices.
 The degree of a vertex i is

(that is, the sum of ith
row)
 For a digraph, the row sum is the out_degree, while
the column sum is the in_degree
∑
j=0
n − 1
adjmat [i ][ j ]
ind ( vi )=∑
j =0
n −1
A [ j ,i ] outd ( vi )=∑
j= 0
n− 1
A [ i , j ]
CHAPTER 6 35
2. Adjacency List
 The n rows of the adjacency matrix are represented as n
chains (either linked lists or array).

That is, replace each row of the adjacency matrix with a
linked list

Every vertex in G will have one list.
CHAPTER 6 36
Data Structures for Adjacency Lists
Each row in adjacency matrix is represented as an adjacency list.
CHAPTER 6 37

G1
0
1 2
3
0
1
2

An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
CHAPTER 6 38
1
0
2
3
4
5
6
7

An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
G4
CHAPTER 6 39
Interesting Operations
degree of a vertex in an undirected graph
–# of nodes in adjacency list
# of edges in a graph
–determined in O(n+e)
out-degree of a vertex in a directed graph
–# of nodes in its adjacency list
in-degree of a vertex in a directed graph
–More complex. Traverse the whole data structure
CHAPTER 6 40
Inverse Adjacency Lists
Useful for finding in-degree of a vertex in
digraphs
Contains one list for each vertex
Each list contains a node for each vertex
adjacent to the vertex that the list represents.
Maintained in addition to
adjacency list.
0
1
2
CHAPTER 6 41
Adjacency Lists – Orthogonal Representation
Alternatively, we can use orthogonal representation
Change the node structure of the adjacency list to
0
1
2
CHAPTER 6 42
Weighted Graph
 In many applications, edges of a graph have weights
assigned to them

Distance or cost or time or delay
 In adjacency matrix, the weights are shown.
 In adjacency list, nodes have an additional field.
Graphs
 Depth First Search
 Breadth First Search
Elementary Graph Operations
Elementary Graph Operations
Just like traversing a tree, we would like to visit all
vertices in a graph.
Just like tree traversal are the most frequently used
operation, in graph also traversal is the most
frequently used operation
Given an undirected graph G = (V, E), and a vertex,
v V
∈ , we wish to visit all vertices in G that are
reachable from v,

That is, all vertices that are connected to v.
CHAPTER 6 45
Some Graph Operations
 Traversal
Given G=(V,E) and vertex v, find all w V, such
∈
that w connects v.
– Depth First Search (DFS)
similar to preorder tree traversal
– Breadth First Search (BFS)
similar to level order tree traversal
Traversal


depth first search: v0, v1, v3, v7, v4, v5, v2, v6
breadth first search: v0, v1, v2, v3, v4, v5, v6, v7
Depth First Search
Depth First Search (DFS) is a graph traversal
algorithm that explores a graph by traversing as far
as possible along each branch before backtracking.
It traverses the graph in a depthward motion and
uses a stack (either explicit or implicit in the recursive
implementation) to keep track of the visited vertices.
It is easy to implement recursively.
CHAPTER 6 48
Depth First Search

Data structure:

adjacency list: O(e)

adjacency matrix: O(n2
)
Depth-First Search Example
Start search at vertex 1.
2
3
8
10
1
4
5 9
11
6
7
Label vertex 1 and do a depth first search
from either 2 or 4.
1
2
Suppose that vertex 2 is selected.
Depth-First Search Example
2
3
8
10
1
4
5 9
11
6
7
Label vertex 2 and do a depth first search
from either 3, 5, or 6.
1
2
2
5
Suppose that vertex 5 is selected.
Depth-First Search Example
2
3
8
10
1
4
5 9
11
6
7
Label vertex 5 and do a depth first search
from either 3, 7, or 9.
1
2
2
5
5 9
Suppose that vertex 9 is selected.
Depth-First Search Example
2
3
8
10
1
4
5 9
11
6
7
Label vertex 9 and do a depth first search
from either 6 or 8.
1
2
2
5
5 9
9
8
Suppose that vertex 8 is selected.
Depth-First Search Example
2
3
8
10
1
4
5 9
11
6
7
Label vertex 8 and return to vertex 9.
1
2
2
5
5 9
9
8
8
From vertex 9 do a DFS(6).
6
Depth-First Search Example
2
3
8
10
1
4
5 9
11
6
7
1
2
2
5
5 9
9
8
8
Label vertex 6 and do a depth first search from either 4 or 7.
6
6
4
Suppose that vertex 4 is selected.
Depth-First Search Example
2
3
8
10
1
4
5 9
11
6
7
1
2
2
5
5 9
9
8
8
Label vertex 4 and return to 6.
6
6
4
4
From vertex 6 do a dfs(7).
7
Depth-First Search Example

2 
3

8

1
0

1

4 
5 
9

11

6 
7

1

2

2

5

5 
9

9

8

8
Label vertex 7 and return to 6.

6

6

4

4

7

7
Return to 9.
Depth-First Search Example

2 
3

8

1
0

1

4 
5 
9

11

6 
7

1

2

2

5

5 
9

9

8

8

6

6

4

4

7

7
Return to 5.
Depth-First Search Example

2 
3

8

1
0

1

4 
5 
9

11

6 
7

1

2

2

5

5 
9

9

8

8

6

6

4

4

7

7
Do a dfs(3).

3
Depth-First Search Example

2 
3

8

1
0

1

4 
5 
9

11

6 
7

1

2

2

5

5 
9

9

8

8

6

6

4

4

7

7
Label 3 and return to 5.

3

3
Return to 2.
Depth-First Search Example

2 
3

8

1
0

1

4 
5 
9

11

6 
7

1

2

2

5

5 
9

9

8

8

6

6

4

4

7

7

3

3
Return to 1.
Depth-First Search Example

2 
3

8

1
0

1

4 
5 
9

11

6 
7

1
2
2

5

5 
9

9

8

8

6

6

4

4

7

7

3

3
Return to invoking method.
Depth First Search
If we represent G by its adjacency lists, then we can
determine the vertices adjacent to v by following a chain of
links.
Since DFS examines each node in the adjacency lists at
most once, the time to complete the search is O(e).
If we represent G by its adjacency matrix, then determining
all vertices adjacent to v requires O(n) time.
Since we visit atmost n vertices, the total time is O(n2
).
Depth First Search
DFS is a fundamental graph traversal algorithm and
forms the basis for many other algorithms and
applications in graph theory and graph-related
problems.
DFS has several applications, including:

Graph Traversal, Connected Components, Cycle
Detection, Path Finding, Topological Sorting (linear
ordering of the vertices that respects the direction of
the edges).
Breadth First Search
Breadth First Search (BFS) is a graph traversal
algorithm that explores all the vertices of a graph in
breadthward motion.
It starts at a chosen vertex and systematically
explores all its neighboring vertices before moving on
to the next level of vertices.
Uses a dynamically linked queue.

Each queue node contains vertex and link fields.


Breadth First Search
Breadth First Search
Breadth First Search
Breadth First Search
BFS explores the graph in a level-by-level manner,
meaning it visits all the vertices at the same distance
(level) from the starting vertex before moving on to
the next level.
BFS guarantees that it will visit all reachable vertices
in the graph and provides the shortest path for
unweighted graphs. However, it requires additional
memory to maintain the queue data structure.
Breadth First Search
BFS can be used to solve various graph-related problems,
including:

Shortest Path, Connected Components, Bipartite Graphs,

Web Crawling, : Explore web pages in a systematic
manner, starting from a given webpage and following links
to other pages.

Social Network Analysis: Explore social networks and
analyze properties such as finding friends of friends,
identifying clusters or communities, or measuring
distances between individuals.
1 de 69

Recomendados

Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs por
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphsFallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphsSnehilKeshari
374 vistas104 diapositivas
chapter6.PPT por
chapter6.PPTchapter6.PPT
chapter6.PPTkrish448427
5 vistas104 diapositivas
Dsa.PPT por
Dsa.PPTDsa.PPT
Dsa.PPTboltkat
11 vistas104 diapositivas
graph.pptx por
graph.pptxgraph.pptx
graph.pptxDEEPAK948083
10 vistas34 diapositivas
Data structure - Graph por
Data structure - GraphData structure - Graph
Data structure - GraphMadhu Bala
6.2K vistas28 diapositivas
DATA STRUCTURES unit 4.pptx por
DATA STRUCTURES unit 4.pptxDATA STRUCTURES unit 4.pptx
DATA STRUCTURES unit 4.pptxShivamKrPathak
35 vistas156 diapositivas

Más contenido relacionado

Similar a 09_DS_MCA_Graphs.pdf

Graphs por
GraphsGraphs
Graphsamudha arul
174 vistas45 diapositivas
Graph representation por
Graph representationGraph representation
Graph representationDEEPIKA T
45 vistas23 diapositivas
Matrix representation of graph por
Matrix representation of graphMatrix representation of graph
Matrix representation of graphRounak Biswas
3.2K vistas83 diapositivas
Chap 6 Graph.ppt por
Chap 6 Graph.pptChap 6 Graph.ppt
Chap 6 Graph.pptshashankbhadouria4
38 vistas115 diapositivas
Grpahs in Data Structure por
Grpahs in Data StructureGrpahs in Data Structure
Grpahs in Data StructureAvichalVishnoi
34 vistas22 diapositivas
Elements of Graph Theory for IS.pptx por
Elements of Graph Theory for IS.pptxElements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptxmiki304759
48 vistas48 diapositivas

Similar a 09_DS_MCA_Graphs.pdf(20)

Graph representation por DEEPIKA T
Graph representationGraph representation
Graph representation
DEEPIKA T45 vistas
Matrix representation of graph por Rounak Biswas
Matrix representation of graphMatrix representation of graph
Matrix representation of graph
Rounak Biswas3.2K vistas
Elements of Graph Theory for IS.pptx por miki304759
Elements of Graph Theory for IS.pptxElements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptx
miki30475948 vistas
Lecture 5b graphs and hashing por Victor Palmar
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
Victor Palmar1.2K vistas
Graph in Data Structure por Prof Ansari
Graph in Data StructureGraph in Data Structure
Graph in Data Structure
Prof Ansari678 vistas
Cs6702 2marks rejinpaul por stalinjothi
Cs6702 2marks rejinpaulCs6702 2marks rejinpaul
Cs6702 2marks rejinpaul
stalinjothi289 vistas
Discrete maths assignment por Keshav Somani
Discrete maths assignmentDiscrete maths assignment
Discrete maths assignment
Keshav Somani576 vistas
Introduction to Graphs por Fulvio Corno
Introduction to GraphsIntroduction to Graphs
Introduction to Graphs
Fulvio Corno703 vistas
Cs6702 graph theory and applications 2 marks questions and answers por appasami
Cs6702 graph theory and applications 2 marks questions and answersCs6702 graph theory and applications 2 marks questions and answers
Cs6702 graph theory and applications 2 marks questions and answers
appasami13.5K vistas

Último

PTicketInput.pdf por
PTicketInput.pdfPTicketInput.pdf
PTicketInput.pdfstuartmcphersonflipm
376 vistas1 diapositiva
Journey of Generative AI por
Journey of Generative AIJourney of Generative AI
Journey of Generative AIthomasjvarghese49
30 vistas37 diapositivas
Organic Shopping in Google Analytics 4.pdf por
Organic Shopping in Google Analytics 4.pdfOrganic Shopping in Google Analytics 4.pdf
Organic Shopping in Google Analytics 4.pdfGA4 Tutorials
10 vistas13 diapositivas
Cross-network in Google Analytics 4.pdf por
Cross-network in Google Analytics 4.pdfCross-network in Google Analytics 4.pdf
Cross-network in Google Analytics 4.pdfGA4 Tutorials
6 vistas7 diapositivas
Short Story Assignment by Kelly Nguyen por
Short Story Assignment by Kelly NguyenShort Story Assignment by Kelly Nguyen
Short Story Assignment by Kelly Nguyenkellynguyen01
18 vistas17 diapositivas
JConWorld_ Continuous SQL with Kafka and Flink por
JConWorld_ Continuous SQL with Kafka and FlinkJConWorld_ Continuous SQL with Kafka and Flink
JConWorld_ Continuous SQL with Kafka and FlinkTimothy Spann
100 vistas36 diapositivas

Último(20)

Organic Shopping in Google Analytics 4.pdf por GA4 Tutorials
Organic Shopping in Google Analytics 4.pdfOrganic Shopping in Google Analytics 4.pdf
Organic Shopping in Google Analytics 4.pdf
GA4 Tutorials10 vistas
Cross-network in Google Analytics 4.pdf por GA4 Tutorials
Cross-network in Google Analytics 4.pdfCross-network in Google Analytics 4.pdf
Cross-network in Google Analytics 4.pdf
GA4 Tutorials6 vistas
Short Story Assignment by Kelly Nguyen por kellynguyen01
Short Story Assignment by Kelly NguyenShort Story Assignment by Kelly Nguyen
Short Story Assignment by Kelly Nguyen
kellynguyen0118 vistas
JConWorld_ Continuous SQL with Kafka and Flink por Timothy Spann
JConWorld_ Continuous SQL with Kafka and FlinkJConWorld_ Continuous SQL with Kafka and Flink
JConWorld_ Continuous SQL with Kafka and Flink
Timothy Spann100 vistas
3196 The Case of The East River por ErickANDRADE90
3196 The Case of The East River3196 The Case of The East River
3196 The Case of The East River
ErickANDRADE9011 vistas
Data structure and algorithm. por Abdul salam
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm.
Abdul salam 18 vistas
Introduction to Microsoft Fabric.pdf por ishaniuudeshika
Introduction to Microsoft Fabric.pdfIntroduction to Microsoft Fabric.pdf
Introduction to Microsoft Fabric.pdf
ishaniuudeshika24 vistas
Advanced_Recommendation_Systems_Presentation.pptx por neeharikasingh29
Advanced_Recommendation_Systems_Presentation.pptxAdvanced_Recommendation_Systems_Presentation.pptx
Advanced_Recommendation_Systems_Presentation.pptx
neeharikasingh295 vistas
UNEP FI CRS Climate Risk Results.pptx por pekka28
UNEP FI CRS Climate Risk Results.pptxUNEP FI CRS Climate Risk Results.pptx
UNEP FI CRS Climate Risk Results.pptx
pekka2811 vistas
Supercharging your Data with Azure AI Search and Azure OpenAI por Peter Gallagher
Supercharging your Data with Azure AI Search and Azure OpenAISupercharging your Data with Azure AI Search and Azure OpenAI
Supercharging your Data with Azure AI Search and Azure OpenAI
Peter Gallagher37 vistas
Launch of the Knowledge Exchange Platform - Romina Boarini - 21 November 2023 por StatsCommunications
Launch of the Knowledge Exchange Platform - Romina Boarini - 21 November 2023Launch of the Knowledge Exchange Platform - Romina Boarini - 21 November 2023
Launch of the Knowledge Exchange Platform - Romina Boarini - 21 November 2023
Survey on Factuality in LLM's.pptx por NeethaSherra1
Survey on Factuality in LLM's.pptxSurvey on Factuality in LLM's.pptx
Survey on Factuality in LLM's.pptx
NeethaSherra15 vistas
Chapter 3b- Process Communication (1) (1)(1) (1).pptx por ayeshabaig2004
Chapter 3b- Process Communication (1) (1)(1) (1).pptxChapter 3b- Process Communication (1) (1)(1) (1).pptx
Chapter 3b- Process Communication (1) (1)(1) (1).pptx
ayeshabaig20045 vistas
Vikas 500 BIG DATA TECHNOLOGIES LAB.pdf por vikas12611618
Vikas 500 BIG DATA TECHNOLOGIES LAB.pdfVikas 500 BIG DATA TECHNOLOGIES LAB.pdf
Vikas 500 BIG DATA TECHNOLOGIES LAB.pdf
vikas126116188 vistas

09_DS_MCA_Graphs.pdf

  • 1. Data Structures Module – 5 (part 1) Graphs:Definitions, Terminologies, Matrix and Adjacency List Representation Of Graphs, Elementary Graph operations, Traversal methods: Breadth First Search and Depth First Search. Insertion Sort, Radix sort, Address Calculation Sort. Hash Table organizations, Hashing Functions, Static and Dynamic Hashing Adopted/Modified for : Data Structures – MCA I Semester Vidya Vikas Institute of Engineering and Technology Mysore 2022-23
  • 3. Graphs A graph is a non-linear data structure consisting of a set of vertices/nodes and a set of edges that connect these vertices. Graphs provide a powerful tool for modeling and solving problems in various domains, including computer networking, social networks, transportation systems, and data analysis. 
  • 4. Graphs - Definition • A graph G = (V, E), where • V is the vertex set. • E is the edge set. – Vertices are also called nodes and points. – Each edge connects two different vertices. – Edges are also called arcs and lines. u v
  • 5. CHAPTER 6 5 Definition  A graph G consists of two sets – a finite, nonempty set of vertices V(G) – a finite, possible empty set of edges E(G) – G(V,E) represents a graph  An undirected graph is one in which the pair of vertices in a edge is unordered, (v0, v1) = (v1,v0)  A directed graph is one in which each edge is a directed pair of vertices, <v0, v1> != <v1,v0> tail head
  • 6. CHAPTER 6 6 Examples for Graph 0 1 2 3 0 1 2 0 1 2 3 4 5 6 G1 G2 G3 V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)} V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)} V(G3)={0,1,2} E(G3)={<0,1>,<1,0>,<1,2>} complete undirected graph: n(n-1)/2 edges complete directed graph: n(n-1) edges complete graph incomplete graph
  • 7. CHAPTER 6 7 Complete Graph  A complete graph is a graph that has the maximum number of edges – for undirected graph with n vertices, the maximum number of edges is n(n-1)/2 – for directed graph with n vertices, the maximum number of edges is n(n-1) – example: G1 is a complete graph
  • 8. CHAPTER 6 8 Adjacent and Incident  If (v0, v1) is an edge in an undirected graph, – v0 and v1 are adjacent – The edge (v0, v1) is incident on vertices v0 and v1  If <v0, v1> is an edge in a directed graph – v0 is adjacent to v1, and v1 is adjacent from v0 – The edge <v0, v1> is incident on v0 and v1
  • 11. Complete Undirected Graph Has all possible edges. n = 1 n = 2 n = 3 n = 4
  • 12. CHAPTER 6 12 0 2 1 (a) 2 1 0 3 (b) self edge multigraph: multiple occurrences of the same edge Figure 6.3  Restrictions on Graph
  • 13. CHAPTER 6 13  A subgraph of G is a graph G’ such that V(G’) is a subset of V(G) and E(G’) is a subset of E(G)  A path from vertex vp to vertex vq in a graph G, is a sequence of vertices, vp, vi1, vi2, ..., vin, vq, such that (vp, vi1), (vi1, vi2), ..., (vin, vq) are edges in an undirected graph  The length of a path is the number of edges on it Subgraph and Path
  • 14. CHAPTER 6 14 0 0 1 2 3 1 2 0 1 2 3 (i) (ii) (iii) (iv) (a) Some of the subgraph of G1 0 0 1 0 1 2 0 1 2 (i) (ii) (iii) (iv) (b) Some of the subgraph of G3 0 1 2 3 G1 0 1 2 G3
  • 15. CHAPTER 6 15  A simple path is a path in which all vertices, except possibly the first and the last, are distinct  A cycle is a simple path in which the first and the last vertices are the same  In an undirected graph G, two vertices, v0 and v1, are connected if there is a path in G from v0 to v1  An undirected graph is connected if, for every pair of distinct vertices vi, vj, there is a path from vi to vj Simple Path and Cycle
  • 16. CHAPTER 6 16 0 1 2 3 0 1 2 3 4 5 6 G1 G2 connected tree (acyclic graph)
  • 17. Path Finding Path between 1 and 8. 2 3 8 10 1 4 5 9 11 6 7 4 8 6 6 7 5 2 4 4 5 3 Path length is 20.
  • 18. Another Path Between 1 and 8 2 3 8 10 1 4 5 9 11 6 7 4 8 6 6 7 5 2 4 4 5 3 Path length is 28.
  • 19. Example Of No Path No path between 2 and 9.  2  3  8  1 0  1  4  5  9  11  6  7
  • 20. Example Of Not Connected  2  3  8  1 0  1  4  5  9  11  6  7
  • 21. Connected Graph Example  2  3  8  1 0  1  4  5  9  11  6  7
  • 22. CHAPTER 6 22  A connected component of an undirected graph is a maximal connected subgraph.  A maximal subgraph is a subgraph within a larger graph that cannot be extended by adding any more vertices or edges while maintaining the subgraph's properties.   A tree is a graph that is connected and acyclic.  Connected Component
  • 24. Not A Component  2  3  8  1 0  1  4  5  9  11  6  7
  • 25. CHAPTER 6 25 A graph with two connected components 1 0 2 3 4 5 6 7 H1 H2 G4 (not connected) connected component (maximal connected subgraph)
  • 26. CHAPTER 6 26  A directed graph is strongly connected if there is a directed path from vi to vj and also from vj to vi.  A strongly connected component is a maximal subgraph that is strongly connected. Connected Component
  • 27. CHAPTER 6 27 Strongly connected components of G3 0 1 2 0 1 2 G3 not strongly connected strongly connected component (maximal strongly connected subgraph)
  • 28. CHAPTER 6 28 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 e di n    ( ) / 0 1 2
  • 29. CHAPTER 6 29 undirected graph degree 0 1 2 3 4 5 6 G1 G2 3 2 3 3 1 1 1 1 directed graph in-degree out-degree 0 1 2 G3 in:1, out: 1 in: 1, out: 2 in: 1, out: 0 0 1 2 3 3 3 3
  • 30. CHAPTER 6 30 ADT for Graph structure Graph is objects: a nonempty set of vertices and a set of undirected edges, where each edge is a pair of vertices functions: for all graph ∈ Graph, v, v1 and v2 ∈ Vertices Graph Create()::=return an empty graph Graph InsertVertex(graph, v)::= return a graph with v inserted. v has no incident edge. Graph InsertEdge(graph, v1,v2)::= return a graph with new edge between v1 and v2 Graph DeleteVertex(graph, v)::= return a graph in which v and all edges incident to it are removed Graph DeleteEdge(graph, v1, v2)::=return a graph in which the edge (v1, v2) is removed Boolean IsEmpty(graph)::= if (graph==empty graph) return TRUE else return FALSE List Adjacent(graph,v)::= return a list of all vertices that are adjacent to v
  • 31. Graphs  Adjacency Matrix  Adjacency Lists Graph Representations
  • 32. CHAPTER 6 32 1. Adjacency Matrix  Let G=(V,E) be a graph with n vertices.  The adjacency matrix of G is a two-dimensional n by n array, say adj_mat  If the edge (vi, vj) is in E(G), adj_mat[i][j]=1  If there is no such edge in E(G), adj_mat[i][j]=0  The adjacency matrix for an undirected graph is symmetric;  The adjacency matrix for a digraph need not be symmetric
  • 33. CHAPTER 6 33 Examples for Adjacency Matrix [ 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 ] [ 0 1 0 1 0 0 0 1 0] [ 0 1 1 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 ] G1 G2 G4 0 1 2 3 0 1 2 1 0 2 3 4 5 6 7 symmetric undirected: n2 /2 directed: n2
  • 34. CHAPTER 6 34 Merits of Adjacency Matrix  From the adjacency matrix, it is easy to determine the connection of vertices.  The degree of a vertex i is  (that is, the sum of ith row)  For a digraph, the row sum is the out_degree, while the column sum is the in_degree ∑ j=0 n − 1 adjmat [i ][ j ] ind ( vi )=∑ j =0 n −1 A [ j ,i ] outd ( vi )=∑ j= 0 n− 1 A [ i , j ]
  • 35. CHAPTER 6 35 2. Adjacency List  The n rows of the adjacency matrix are represented as n chains (either linked lists or array).  That is, replace each row of the adjacency matrix with a linked list  Every vertex in G will have one list.
  • 36. CHAPTER 6 36 Data Structures for Adjacency Lists Each row in adjacency matrix is represented as an adjacency list.
  • 37. CHAPTER 6 37  G1 0 1 2 3 0 1 2  An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
  • 38. CHAPTER 6 38 1 0 2 3 4 5 6 7  An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes G4
  • 39. CHAPTER 6 39 Interesting Operations degree of a vertex in an undirected graph –# of nodes in adjacency list # of edges in a graph –determined in O(n+e) out-degree of a vertex in a directed graph –# of nodes in its adjacency list in-degree of a vertex in a directed graph –More complex. Traverse the whole data structure
  • 40. CHAPTER 6 40 Inverse Adjacency Lists Useful for finding in-degree of a vertex in digraphs Contains one list for each vertex Each list contains a node for each vertex adjacent to the vertex that the list represents. Maintained in addition to adjacency list. 0 1 2
  • 41. CHAPTER 6 41 Adjacency Lists – Orthogonal Representation Alternatively, we can use orthogonal representation Change the node structure of the adjacency list to 0 1 2
  • 42. CHAPTER 6 42 Weighted Graph  In many applications, edges of a graph have weights assigned to them  Distance or cost or time or delay  In adjacency matrix, the weights are shown.  In adjacency list, nodes have an additional field.
  • 43. Graphs  Depth First Search  Breadth First Search Elementary Graph Operations
  • 44. Elementary Graph Operations Just like traversing a tree, we would like to visit all vertices in a graph. Just like tree traversal are the most frequently used operation, in graph also traversal is the most frequently used operation Given an undirected graph G = (V, E), and a vertex, v V ∈ , we wish to visit all vertices in G that are reachable from v,  That is, all vertices that are connected to v.
  • 45. CHAPTER 6 45 Some Graph Operations  Traversal Given G=(V,E) and vertex v, find all w V, such ∈ that w connects v. – Depth First Search (DFS) similar to preorder tree traversal – Breadth First Search (BFS) similar to level order tree traversal
  • 46. Traversal   depth first search: v0, v1, v3, v7, v4, v5, v2, v6 breadth first search: v0, v1, v2, v3, v4, v5, v6, v7
  • 47. Depth First Search Depth First Search (DFS) is a graph traversal algorithm that explores a graph by traversing as far as possible along each branch before backtracking. It traverses the graph in a depthward motion and uses a stack (either explicit or implicit in the recursive implementation) to keep track of the visited vertices. It is easy to implement recursively.
  • 48. CHAPTER 6 48 Depth First Search  Data structure:  adjacency list: O(e)  adjacency matrix: O(n2 )
  • 49. Depth-First Search Example Start search at vertex 1. 2 3 8 10 1 4 5 9 11 6 7 Label vertex 1 and do a depth first search from either 2 or 4. 1 2 Suppose that vertex 2 is selected.
  • 50. Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Label vertex 2 and do a depth first search from either 3, 5, or 6. 1 2 2 5 Suppose that vertex 5 is selected.
  • 51. Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Label vertex 5 and do a depth first search from either 3, 7, or 9. 1 2 2 5 5 9 Suppose that vertex 9 is selected.
  • 52. Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Label vertex 9 and do a depth first search from either 6 or 8. 1 2 2 5 5 9 9 8 Suppose that vertex 8 is selected.
  • 53. Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Label vertex 8 and return to vertex 9. 1 2 2 5 5 9 9 8 8 From vertex 9 do a DFS(6). 6
  • 54. Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 1 2 2 5 5 9 9 8 8 Label vertex 6 and do a depth first search from either 4 or 7. 6 6 4 Suppose that vertex 4 is selected.
  • 55. Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 1 2 2 5 5 9 9 8 8 Label vertex 4 and return to 6. 6 6 4 4 From vertex 6 do a dfs(7). 7
  • 56. Depth-First Search Example  2  3  8  1 0  1  4  5  9  11  6  7  1  2  2  5  5  9  9  8  8 Label vertex 7 and return to 6.  6  6  4  4  7  7 Return to 9.
  • 57. Depth-First Search Example  2  3  8  1 0  1  4  5  9  11  6  7  1  2  2  5  5  9  9  8  8  6  6  4  4  7  7 Return to 5.
  • 58. Depth-First Search Example  2  3  8  1 0  1  4  5  9  11  6  7  1  2  2  5  5  9  9  8  8  6  6  4  4  7  7 Do a dfs(3).  3
  • 59. Depth-First Search Example  2  3  8  1 0  1  4  5  9  11  6  7  1  2  2  5  5  9  9  8  8  6  6  4  4  7  7 Label 3 and return to 5.  3  3 Return to 2.
  • 60. Depth-First Search Example  2  3  8  1 0  1  4  5  9  11  6  7  1  2  2  5  5  9  9  8  8  6  6  4  4  7  7  3  3 Return to 1.
  • 61. Depth-First Search Example  2  3  8  1 0  1  4  5  9  11  6  7  1 2 2  5  5  9  9  8  8  6  6  4  4  7  7  3  3 Return to invoking method.
  • 62. Depth First Search If we represent G by its adjacency lists, then we can determine the vertices adjacent to v by following a chain of links. Since DFS examines each node in the adjacency lists at most once, the time to complete the search is O(e). If we represent G by its adjacency matrix, then determining all vertices adjacent to v requires O(n) time. Since we visit atmost n vertices, the total time is O(n2 ).
  • 63. Depth First Search DFS is a fundamental graph traversal algorithm and forms the basis for many other algorithms and applications in graph theory and graph-related problems. DFS has several applications, including:  Graph Traversal, Connected Components, Cycle Detection, Path Finding, Topological Sorting (linear ordering of the vertices that respects the direction of the edges).
  • 64. Breadth First Search Breadth First Search (BFS) is a graph traversal algorithm that explores all the vertices of a graph in breadthward motion. It starts at a chosen vertex and systematically explores all its neighboring vertices before moving on to the next level of vertices. Uses a dynamically linked queue.  Each queue node contains vertex and link fields.
  • 68. Breadth First Search BFS explores the graph in a level-by-level manner, meaning it visits all the vertices at the same distance (level) from the starting vertex before moving on to the next level. BFS guarantees that it will visit all reachable vertices in the graph and provides the shortest path for unweighted graphs. However, it requires additional memory to maintain the queue data structure.
  • 69. Breadth First Search BFS can be used to solve various graph-related problems, including:  Shortest Path, Connected Components, Bipartite Graphs,  Web Crawling, : Explore web pages in a systematic manner, starting from a given webpage and following links to other pages.  Social Network Analysis: Explore social networks and analyze properties such as finding friends of friends, identifying clusters or communities, or measuring distances between individuals.