SlideShare a Scribd company logo
1 of 34
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
1
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 Graphs as one of the most important non-linear data
structures
 The representation that models various kinds of
graphs
 Some useful graph algorithms
2
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 A graph G is a discrete structure consisting of nodes (vertices)
and the lines joining the nodes (edges)
 For finite graphs, V and E are finite. We can write a graph as G =
(V, E)
Graph traversals—
Visiting all the vertices and edges in a systematic fashion is
called as a graph traversal
3
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 Graphs are classified as directed and undirected graphs
 In an undirected graph, an edge is a set of two vertices where
order does not make any relevance, whereas in a directed graph,
an edge is an ordered pair
4
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 Graph
5
create() :Graph
insert vertex(Graph, v) :Graph
delete vertex(Graph, v) :Graph
insert edge(Graph, u, v) :Graph
delete edge(Graph, u, v) :Graph
is empty(Graph) :Boolean;
end graph
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 Adjacency matrix (sequential representation)
 Adjacency list (linked representation)
 Adjacency Multilist
 Inverse Adjacency List
6
Representation of
Graphs
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 The graphs represented using a sequential
representation using matrices is called an adjacency
matrix
7
Adjacency Matrix
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
8
Adjacency Matrix
representation
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 In an adjacency list, the n rows of the adjacency list are
represented as n-linked lists, one list per vertex of the graph
 We can represent G by an array Head, where Head[i] is a pointer
to the adjacency list of vertex i
 Each node of the list has at least two fields: vertex and link
 The vertex field contains the vertex–id, and link field stores the
pointer to the next node storing another vertex adjacent to i
9
Adjacency Lists
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
10
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
11
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
12
 Multiclass are lists where nodes may be
shared among several other lists
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 The node structure of such a list can be
represented as follows :
13
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
14
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 Inverse adjacency lists is a set of lists that contain
one list for vertex
 Each list contains a node per vertex adjacent to the
vertex it represents
15
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
16
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 DFS starts at the vertex v of G as a start vertex and v is
marked as visited
 Then, each unvisited vertex adjacent to v is searched
using the DFS recursively
 Once all the vertices that can be reached from v have
been visited, the search of v is complete
 If some vertices remain unvisited, we select an
unvisited vertex as a new start vertex and then repeat
the process until all the vertices of G are marked
visited
17
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
18
(a) Graph and its (b) adjacency list
representation
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 A tree is a connected graph with no cycles
 A spanning tree is a sub-graph of G that has all
vertices of G and is a tree
 A minimum spanning tree of a weighted graph G is
the spanning tree of G whose edges sum to minimum
weight
19
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 In BFS, all the unvisited vertices adjacent to i are
visited after visiting the start vertex i and marking it
visited
 Next, the unvisited vertices adjacent to these vertices
are visited and so on until the entire graph has been
traversed
20
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
21
Breadth-first search sequence for the
graph
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 A tree is a connected graph with no cycles
 A spanning tree is a sub-graph of G that has all
vertices of G and is a tree
 A minimum spanning tree of a weighted graph G is
the spanning tree of G whose edges sum to minimum
weight
22
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
23
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 An undirected graph is connected if there is at least one path
between every pair of vertices in the graph
 A connected component of a graph is a maximal connected sub-
graph, that is, every vertex in a connected component is
reachable from the vertices in the component
24
Connected Components
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 Following fig shows the Sample graph with one connected
component
25
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 Following fig shows the Graph with two connected
components
26
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 Prim’s algorithm starts from one vertex and grows the rest of the
tree by adding one vertex at a time, by adding the associated
edges
 This algorithm builds a tree by iteratively adding edges until a
minimal spanning tree is obtained, that is, when all nodes are
added
 At each iteration, it adds to the current tree a vertex though
minimum weight edge that does not complete a cycle
27
Prim’s Algorithm
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
28
Minimum Spanning Tree
Minimum
Spanning Tree
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 Another way to construct a minimum spanning tree for a graph
G is to start with a graph T = (V′, E′ = ø) consisting of the n
vertices of G and having no edges
 In Prim’s algorithm, we start with one connected component,
add a vertex to have one connected component and no cycles,
and end up with one connected component
 Here, we start with n connected components; at each step, the
number of connected components would reduce by one and end
up with one connected component
29
Kruskal’s Algorithm
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 The shortest path between two given vertices is the path having
minimum length
 This problem can be solved by one of the greedy algorithms, by
Edger W. Dijkstra, often called as Dijkstra’s algorithm
30
Shortest Path Algorithm
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
31
Shortest Path Algorithm
Directed weighted graph
Shortest Path
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 Graphs are one of the most important non-linear data structures. A
graph is a representation of relation
 Vertices represent elements and edges represent relationships
 In other words, a graph is a collection of nodes (vertices) and arcs
joining pair of the nodes (edges)
 Graphs are classified as directed and undirected graphs. In an
undirected graph, an edge is a set of two vertices where order does
not make any relevance, whereas in a directed graph, an edge is an
ordered pair.
32
Summary
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
 Graphs are implemented using an array or a linked list representation
 An adjacency list is a data structure for representing a graph by keeping a list
of the neighbour vertices for each vertex
 An adjacency matrix is a data structure for representing a graph as a Boolean
matrix where 0 means no edge and 1 corresponds to an edge
 A minimum spanning tree is a tree, containing all the vertices of a
graph, where the total weight of the edges is minimum
 The two popularly used algorithms to compute minimum spanning tree are the
following: Prim’s and Kruskal’s algorithms
 Dijkstra’s algorithm is another common algorithm for graphs to find the
shortest path between two vertices of a graph
33
Summary
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
End
of
Chapter 8…!
34

More Related Content

What's hot

Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structureVardhil Patel
 
Graph representation
Graph representationGraph representation
Graph representationTech_MX
 
Graphs in data structure
Graphs in data structureGraphs in data structure
Graphs in data structurehamza javed
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IMohamed Loey
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data StructureAnuj Modi
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structuresSavit Chandra
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST Kathirvel Ayyaswamy
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort AlgorithmLemia Algmri
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSGayathri Gaayu
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanDaniyal Khan
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
 

What's hot (20)

Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structure
 
Graph representation
Graph representationGraph representation
Graph representation
 
Graphs in data structure
Graphs in data structureGraphs in data structure
Graphs in data structure
 
Graph theory
Graph  theoryGraph  theory
Graph theory
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Graph theory
Graph theory Graph theory
Graph theory
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data Structure
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
single linked list
single linked listsingle linked list
single linked list
 

Viewers also liked

7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...widespreadpromotion
 
11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patil11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patil1. Fundamental Concept - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
មេរៀនៈ Data Structure and Algorithm in C/C++
មេរៀនៈ Data Structure and Algorithm in C/C++មេរៀនៈ Data Structure and Algorithm in C/C++
មេរៀនៈ Data Structure and Algorithm in C/C++Ngeam Soly
 
Ds 111011055724-phpapp01
Ds 111011055724-phpapp01Ds 111011055724-phpapp01
Ds 111011055724-phpapp01Getachew Ganfur
 
Question answering in linked data
Question answering in linked dataQuestion answering in linked data
Question answering in linked dataReza Ramezani
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructorDa Mystic Sadi
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueRai University
 
The State of Twitter: STL 2011
The State of Twitter: STL 2011The State of Twitter: STL 2011
The State of Twitter: STL 2011Infuz
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1Vineeta Garg
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsVineeta Garg
 
Advanced data structures using c++ 3
Advanced data structures using c++ 3Advanced data structures using c++ 3
Advanced data structures using c++ 3Shaili Choudhary
 

Viewers also liked (20)

7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil
 
5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil
 
6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil
 
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
 
3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil
 
10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil
 
4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil
 
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
 
11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patil11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patil
 
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patil1. Fundamental Concept - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
 
មេរៀនៈ Data Structure and Algorithm in C/C++
មេរៀនៈ Data Structure and Algorithm in C/C++មេរៀនៈ Data Structure and Algorithm in C/C++
មេរៀនៈ Data Structure and Algorithm in C/C++
 
Ds 111011055724-phpapp01
Ds 111011055724-phpapp01Ds 111011055724-phpapp01
Ds 111011055724-phpapp01
 
Stack & Queue
Stack & QueueStack & Queue
Stack & Queue
 
Question answering in linked data
Question answering in linked dataQuestion answering in linked data
Question answering in linked data
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructor
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
The State of Twitter: STL 2011
The State of Twitter: STL 2011The State of Twitter: STL 2011
The State of Twitter: STL 2011
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Advanced data structures using c++ 3
Advanced data structures using c++ 3Advanced data structures using c++ 3
Advanced data structures using c++ 3
 

Similar to 8. Graph - Data Structures using C++ by Varsha Patil

NON-LINEAR DATA STRUCTURE-Graphs.pptx
NON-LINEAR DATA STRUCTURE-Graphs.pptxNON-LINEAR DATA STRUCTURE-Graphs.pptx
NON-LINEAR DATA STRUCTURE-Graphs.pptxRajitha Reddy Alugati
 
Slides Chapter10.1 10.2
Slides Chapter10.1 10.2Slides Chapter10.1 10.2
Slides Chapter10.1 10.2showslidedump
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxasimshahzad8611
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxking779879
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashingVictor Palmar
 
Graph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptxGraph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptxsahilpawar2426
 
PPT on Graph Theory ( Tree, Cotree, nodes, branches, incidence , tie set and ...
PPT on Graph Theory ( Tree, Cotree, nodes, branches, incidence , tie set and ...PPT on Graph Theory ( Tree, Cotree, nodes, branches, incidence , tie set and ...
PPT on Graph Theory ( Tree, Cotree, nodes, branches, incidence , tie set and ...SUTAPAMUKHERJEE12
 
Semantic Data Management in Graph Databases
Semantic Data Management in Graph DatabasesSemantic Data Management in Graph Databases
Semantic Data Management in Graph DatabasesMaribel Acosta Deibe
 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structureMahmoud Alfarra
 
Graph Introduction.ppt
Graph Introduction.pptGraph Introduction.ppt
Graph Introduction.pptFaruk Hossen
 
Graph theory concepts complex networks presents-rouhollah nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabatinabati
 

Similar to 8. Graph - Data Structures using C++ by Varsha Patil (20)

Spanningtreesppt
SpanningtreespptSpanningtreesppt
Spanningtreesppt
 
NON-LINEAR DATA STRUCTURE-Graphs.pptx
NON-LINEAR DATA STRUCTURE-Graphs.pptxNON-LINEAR DATA STRUCTURE-Graphs.pptx
NON-LINEAR DATA STRUCTURE-Graphs.pptx
 
Ch18
Ch18Ch18
Ch18
 
Slides Chapter10.1 10.2
Slides Chapter10.1 10.2Slides Chapter10.1 10.2
Slides Chapter10.1 10.2
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptx
 
Graph data structure and algorithms
Graph data structure and algorithmsGraph data structure and algorithms
Graph data structure and algorithms
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
 
Graph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptxGraph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptx
 
Graph.pptx
Graph.pptxGraph.pptx
Graph.pptx
 
09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf
 
PPT on Graph Theory ( Tree, Cotree, nodes, branches, incidence , tie set and ...
PPT on Graph Theory ( Tree, Cotree, nodes, branches, incidence , tie set and ...PPT on Graph Theory ( Tree, Cotree, nodes, branches, incidence , tie set and ...
PPT on Graph Theory ( Tree, Cotree, nodes, branches, incidence , tie set and ...
 
H0431038048
H0431038048H0431038048
H0431038048
 
Graphs.pptx
Graphs.pptxGraphs.pptx
Graphs.pptx
 
Semantic Data Management in Graph Databases
Semantic Data Management in Graph DatabasesSemantic Data Management in Graph Databases
Semantic Data Management in Graph Databases
 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structure
 
Basics of graph
Basics of graphBasics of graph
Basics of graph
 
Graph Introduction.ppt
Graph Introduction.pptGraph Introduction.ppt
Graph Introduction.ppt
 
Graph theory concepts complex networks presents-rouhollah nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabati
 
Dijkstra
DijkstraDijkstra
Dijkstra
 

More from widespreadpromotion

Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. PatilDiscrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patilwidespreadpromotion
 
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
15. STL - Data Structures using C++ by Varsha Patil
15. STL - Data Structures using C++ by Varsha Patil15. STL - Data Structures using C++ by Varsha Patil
15. STL - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
14. Files - Data Structures using C++ by Varsha Patil
14. Files - Data Structures using C++ by Varsha Patil14. Files - Data Structures using C++ by Varsha Patil
14. Files - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
12. Heaps - Data Structures using C++ by Varsha Patil
12. Heaps - Data Structures using C++ by Varsha Patil12. Heaps - Data Structures using C++ by Varsha Patil
12. Heaps - Data Structures using C++ by Varsha Patilwidespreadpromotion
 

More from widespreadpromotion (6)

Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. PatilDiscrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
 
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
 
15. STL - Data Structures using C++ by Varsha Patil
15. STL - Data Structures using C++ by Varsha Patil15. STL - Data Structures using C++ by Varsha Patil
15. STL - Data Structures using C++ by Varsha Patil
 
14. Files - Data Structures using C++ by Varsha Patil
14. Files - Data Structures using C++ by Varsha Patil14. Files - Data Structures using C++ by Varsha Patil
14. Files - Data Structures using C++ by Varsha Patil
 
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
 
12. Heaps - Data Structures using C++ by Varsha Patil
12. Heaps - Data Structures using C++ by Varsha Patil12. Heaps - Data Structures using C++ by Varsha Patil
12. Heaps - Data Structures using C++ by Varsha Patil
 

Recently uploaded

Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...
Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...
Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...Boston Institute of Analytics
 
What To Do For World Nature Conservation Day by Slidesgo.pptx
What To Do For World Nature Conservation Day by Slidesgo.pptxWhat To Do For World Nature Conservation Day by Slidesgo.pptx
What To Do For World Nature Conservation Day by Slidesgo.pptxSimranPal17
 
Real-Time AI Streaming - AI Max Princeton
Real-Time AI  Streaming - AI Max PrincetonReal-Time AI  Streaming - AI Max Princeton
Real-Time AI Streaming - AI Max PrincetonTimothy Spann
 
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdfEnglish-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdfblazblazml
 
Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Seán Kennedy
 
Principles and Practices of Data Visualization
Principles and Practices of Data VisualizationPrinciples and Practices of Data Visualization
Principles and Practices of Data VisualizationKianJazayeri1
 
FAIR, FAIRsharing, FAIR Cookbook and ELIXIR - Sansone SA - Boston 2024
FAIR, FAIRsharing, FAIR Cookbook and ELIXIR - Sansone SA - Boston 2024FAIR, FAIRsharing, FAIR Cookbook and ELIXIR - Sansone SA - Boston 2024
FAIR, FAIRsharing, FAIR Cookbook and ELIXIR - Sansone SA - Boston 2024Susanna-Assunta Sansone
 
wepik-insightful-infographics-a-data-visualization-overview-20240401133220kwr...
wepik-insightful-infographics-a-data-visualization-overview-20240401133220kwr...wepik-insightful-infographics-a-data-visualization-overview-20240401133220kwr...
wepik-insightful-infographics-a-data-visualization-overview-20240401133220kwr...KarteekMane1
 
Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Seán Kennedy
 
Advanced Machine Learning for Business Professionals
Advanced Machine Learning for Business ProfessionalsAdvanced Machine Learning for Business Professionals
Advanced Machine Learning for Business ProfessionalsVICTOR MAESTRE RAMIREZ
 
Bank Loan Approval Analysis: A Comprehensive Data Analysis Project
Bank Loan Approval Analysis: A Comprehensive Data Analysis ProjectBank Loan Approval Analysis: A Comprehensive Data Analysis Project
Bank Loan Approval Analysis: A Comprehensive Data Analysis ProjectBoston Institute of Analytics
 
Cyber awareness ppt on the recorded data
Cyber awareness ppt on the recorded dataCyber awareness ppt on the recorded data
Cyber awareness ppt on the recorded dataTecnoIncentive
 
Digital Marketing Plan, how digital marketing works
Digital Marketing Plan, how digital marketing worksDigital Marketing Plan, how digital marketing works
Digital Marketing Plan, how digital marketing worksdeepakthakur548787
 
Decoding Patterns: Customer Churn Prediction Data Analysis Project
Decoding Patterns: Customer Churn Prediction Data Analysis ProjectDecoding Patterns: Customer Churn Prediction Data Analysis Project
Decoding Patterns: Customer Churn Prediction Data Analysis ProjectBoston Institute of Analytics
 
INTRODUCTION TO Natural language processing
INTRODUCTION TO Natural language processingINTRODUCTION TO Natural language processing
INTRODUCTION TO Natural language processingsocarem879
 
SMOTE and K-Fold Cross Validation-Presentation.pptx
SMOTE and K-Fold Cross Validation-Presentation.pptxSMOTE and K-Fold Cross Validation-Presentation.pptx
SMOTE and K-Fold Cross Validation-Presentation.pptxHaritikaChhatwal1
 
Networking Case Study prepared by teacher.pptx
Networking Case Study prepared by teacher.pptxNetworking Case Study prepared by teacher.pptx
Networking Case Study prepared by teacher.pptxHimangsuNath
 
Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 217djon017
 

Recently uploaded (20)

Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...
Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...
Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...
 
What To Do For World Nature Conservation Day by Slidesgo.pptx
What To Do For World Nature Conservation Day by Slidesgo.pptxWhat To Do For World Nature Conservation Day by Slidesgo.pptx
What To Do For World Nature Conservation Day by Slidesgo.pptx
 
Real-Time AI Streaming - AI Max Princeton
Real-Time AI  Streaming - AI Max PrincetonReal-Time AI  Streaming - AI Max Princeton
Real-Time AI Streaming - AI Max Princeton
 
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdfEnglish-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
English-8-Q4-W3-Synthesizing-Essential-Information-From-Various-Sources-1.pdf
 
Insurance Churn Prediction Data Analysis Project
Insurance Churn Prediction Data Analysis ProjectInsurance Churn Prediction Data Analysis Project
Insurance Churn Prediction Data Analysis Project
 
Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...
 
Principles and Practices of Data Visualization
Principles and Practices of Data VisualizationPrinciples and Practices of Data Visualization
Principles and Practices of Data Visualization
 
FAIR, FAIRsharing, FAIR Cookbook and ELIXIR - Sansone SA - Boston 2024
FAIR, FAIRsharing, FAIR Cookbook and ELIXIR - Sansone SA - Boston 2024FAIR, FAIRsharing, FAIR Cookbook and ELIXIR - Sansone SA - Boston 2024
FAIR, FAIRsharing, FAIR Cookbook and ELIXIR - Sansone SA - Boston 2024
 
wepik-insightful-infographics-a-data-visualization-overview-20240401133220kwr...
wepik-insightful-infographics-a-data-visualization-overview-20240401133220kwr...wepik-insightful-infographics-a-data-visualization-overview-20240401133220kwr...
wepik-insightful-infographics-a-data-visualization-overview-20240401133220kwr...
 
Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...
 
Advanced Machine Learning for Business Professionals
Advanced Machine Learning for Business ProfessionalsAdvanced Machine Learning for Business Professionals
Advanced Machine Learning for Business Professionals
 
Bank Loan Approval Analysis: A Comprehensive Data Analysis Project
Bank Loan Approval Analysis: A Comprehensive Data Analysis ProjectBank Loan Approval Analysis: A Comprehensive Data Analysis Project
Bank Loan Approval Analysis: A Comprehensive Data Analysis Project
 
Cyber awareness ppt on the recorded data
Cyber awareness ppt on the recorded dataCyber awareness ppt on the recorded data
Cyber awareness ppt on the recorded data
 
Digital Marketing Plan, how digital marketing works
Digital Marketing Plan, how digital marketing worksDigital Marketing Plan, how digital marketing works
Digital Marketing Plan, how digital marketing works
 
Decoding Patterns: Customer Churn Prediction Data Analysis Project
Decoding Patterns: Customer Churn Prediction Data Analysis ProjectDecoding Patterns: Customer Churn Prediction Data Analysis Project
Decoding Patterns: Customer Churn Prediction Data Analysis Project
 
Data Analysis Project: Stroke Prediction
Data Analysis Project: Stroke PredictionData Analysis Project: Stroke Prediction
Data Analysis Project: Stroke Prediction
 
INTRODUCTION TO Natural language processing
INTRODUCTION TO Natural language processingINTRODUCTION TO Natural language processing
INTRODUCTION TO Natural language processing
 
SMOTE and K-Fold Cross Validation-Presentation.pptx
SMOTE and K-Fold Cross Validation-Presentation.pptxSMOTE and K-Fold Cross Validation-Presentation.pptx
SMOTE and K-Fold Cross Validation-Presentation.pptx
 
Networking Case Study prepared by teacher.pptx
Networking Case Study prepared by teacher.pptxNetworking Case Study prepared by teacher.pptx
Networking Case Study prepared by teacher.pptx
 
Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2
 

8. Graph - Data Structures using C++ by Varsha Patil

  • 1. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 1
  • 2. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  Graphs as one of the most important non-linear data structures  The representation that models various kinds of graphs  Some useful graph algorithms 2
  • 3. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  A graph G is a discrete structure consisting of nodes (vertices) and the lines joining the nodes (edges)  For finite graphs, V and E are finite. We can write a graph as G = (V, E) Graph traversals— Visiting all the vertices and edges in a systematic fashion is called as a graph traversal 3
  • 4. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  Graphs are classified as directed and undirected graphs  In an undirected graph, an edge is a set of two vertices where order does not make any relevance, whereas in a directed graph, an edge is an ordered pair 4
  • 5. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  Graph 5 create() :Graph insert vertex(Graph, v) :Graph delete vertex(Graph, v) :Graph insert edge(Graph, u, v) :Graph delete edge(Graph, u, v) :Graph is empty(Graph) :Boolean; end graph
  • 6. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  Adjacency matrix (sequential representation)  Adjacency list (linked representation)  Adjacency Multilist  Inverse Adjacency List 6 Representation of Graphs
  • 7. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  The graphs represented using a sequential representation using matrices is called an adjacency matrix 7 Adjacency Matrix
  • 8. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 8 Adjacency Matrix representation
  • 9. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  In an adjacency list, the n rows of the adjacency list are represented as n-linked lists, one list per vertex of the graph  We can represent G by an array Head, where Head[i] is a pointer to the adjacency list of vertex i  Each node of the list has at least two fields: vertex and link  The vertex field contains the vertex–id, and link field stores the pointer to the next node storing another vertex adjacent to i 9 Adjacency Lists
  • 10. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 10
  • 11. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 11
  • 12. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 12  Multiclass are lists where nodes may be shared among several other lists
  • 13. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  The node structure of such a list can be represented as follows : 13
  • 14. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 14
  • 15. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  Inverse adjacency lists is a set of lists that contain one list for vertex  Each list contains a node per vertex adjacent to the vertex it represents 15
  • 16. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 16
  • 17. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  DFS starts at the vertex v of G as a start vertex and v is marked as visited  Then, each unvisited vertex adjacent to v is searched using the DFS recursively  Once all the vertices that can be reached from v have been visited, the search of v is complete  If some vertices remain unvisited, we select an unvisited vertex as a new start vertex and then repeat the process until all the vertices of G are marked visited 17
  • 18. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 18 (a) Graph and its (b) adjacency list representation
  • 19. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  A tree is a connected graph with no cycles  A spanning tree is a sub-graph of G that has all vertices of G and is a tree  A minimum spanning tree of a weighted graph G is the spanning tree of G whose edges sum to minimum weight 19
  • 20. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  In BFS, all the unvisited vertices adjacent to i are visited after visiting the start vertex i and marking it visited  Next, the unvisited vertices adjacent to these vertices are visited and so on until the entire graph has been traversed 20
  • 21. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 21 Breadth-first search sequence for the graph
  • 22. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  A tree is a connected graph with no cycles  A spanning tree is a sub-graph of G that has all vertices of G and is a tree  A minimum spanning tree of a weighted graph G is the spanning tree of G whose edges sum to minimum weight 22
  • 23. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 23
  • 24. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  An undirected graph is connected if there is at least one path between every pair of vertices in the graph  A connected component of a graph is a maximal connected sub- graph, that is, every vertex in a connected component is reachable from the vertices in the component 24 Connected Components
  • 25. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  Following fig shows the Sample graph with one connected component 25
  • 26. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  Following fig shows the Graph with two connected components 26
  • 27. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  Prim’s algorithm starts from one vertex and grows the rest of the tree by adding one vertex at a time, by adding the associated edges  This algorithm builds a tree by iteratively adding edges until a minimal spanning tree is obtained, that is, when all nodes are added  At each iteration, it adds to the current tree a vertex though minimum weight edge that does not complete a cycle 27 Prim’s Algorithm
  • 28. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 28 Minimum Spanning Tree Minimum Spanning Tree
  • 29. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  Another way to construct a minimum spanning tree for a graph G is to start with a graph T = (V′, E′ = ø) consisting of the n vertices of G and having no edges  In Prim’s algorithm, we start with one connected component, add a vertex to have one connected component and no cycles, and end up with one connected component  Here, we start with n connected components; at each step, the number of connected components would reduce by one and end up with one connected component 29 Kruskal’s Algorithm
  • 30. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  The shortest path between two given vertices is the path having minimum length  This problem can be solved by one of the greedy algorithms, by Edger W. Dijkstra, often called as Dijkstra’s algorithm 30 Shortest Path Algorithm
  • 31. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 31 Shortest Path Algorithm Directed weighted graph Shortest Path
  • 32. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  Graphs are one of the most important non-linear data structures. A graph is a representation of relation  Vertices represent elements and edges represent relationships  In other words, a graph is a collection of nodes (vertices) and arcs joining pair of the nodes (edges)  Graphs are classified as directed and undirected graphs. In an undirected graph, an edge is a set of two vertices where order does not make any relevance, whereas in a directed graph, an edge is an ordered pair. 32 Summary
  • 33. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil  Graphs are implemented using an array or a linked list representation  An adjacency list is a data structure for representing a graph by keeping a list of the neighbour vertices for each vertex  An adjacency matrix is a data structure for representing a graph as a Boolean matrix where 0 means no edge and 1 corresponds to an edge  A minimum spanning tree is a tree, containing all the vertices of a graph, where the total weight of the edges is minimum  The two popularly used algorithms to compute minimum spanning tree are the following: Prim’s and Kruskal’s algorithms  Dijkstra’s algorithm is another common algorithm for graphs to find the shortest path between two vertices of a graph 33 Summary
  • 34. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil End of Chapter 8…! 34