SlideShare a Scribd company logo
1 of 17
Graph
What is a Graph?
 A data Structure that consists of a set of
nodes(vertices) and a set of edges that relate the node
to each other.
 The set of edges describes relationship among the
vertices.
edge
Node
Formal Definition Of Graphs
 A graph G is defined as follows:
G=(V,E)
V: set of vertices
E:set of edges connecting the vertices in V
An edge e=(u , v) is a pair of vertices
Example:
a b
c d
e
V={a,bc,d,e}
E={(a,b),(a,c),(a,d),(b,e)
,(c,d),(c,e),(d,e)}
 When the edges in a graph have no direction , the
graph is called undirected.
 When the edges in a graph have a direction , the graph
is called directed.
Undirected edge Directed edge
a b a b
1
2
5
4
3
1
2
5
4
3
Undirected Graph Directed Graph
 Adjacent nodes : two nodes are adjacent if they are
connected by an edge.
 Path : a sequence of vertices that connect two nodes
in a graph
 Simple path : A simple path is a path in which all
vertices , except possibly in the first and last ,are
different.
 Complete Graph: a graph in which every vertex is
directly connected to every other vertex
a b
A is adjacent to b
 Adjacency matrix representation.
 Adjacency lists representation.
Adjacency matrix
Let G=(V , E) be a graph with n vertices.
The adjacency matrix of G is a two dimensional array n by
n , say A[5][5].
A B
C D
E
(A) 1
(B) 2
(C) 3
(D)4
(E)5
(A)1 (B)2 (C)3 (D)4 (E)5
0 1 1 1 0
01 0 1 0
1 0 0 1 1
1 1 1 0 1
0 0 1 1 0
When the adjacent vertices of a graph is represented in the form of matrix
then such type of representation is called Adjacency Matrix
Representation of graph. E.g. A[i][j]
Number of rows = Number of Column
A ij =
O , if Vi is not adjacent to Vj
1 , if Vi , Vj is adjacent
Adjacency lists
B C
E
A D
NODE ADJACENCY LIST
A B , C , D
B C
C NULL
D C , E
E C
When the vertices of graph are represented by the linked
list then such type of representation is called adjacency list
representation of graph.
Traverse all node in the graph.
Methods
* Breadth – First Search
*Depth - First Search
Breadth First Search(BFS)
*BFS uses queue.
Logic
*During execution of algorithms , each node N of G will be
in one of three states called status of N:
- STATUS = 1(Ready state)
- STATUS = 2(Waiting state)
- STATUS= 3(Processed state)
ED
C
F
A
B
Algorithm
1.Initialize all nodes to the ready state
(STATUS=1)
2.Put the starting node A in Queue and
change its status to the waiting state
(STATUS=2)
3. [LOOP]
Repeat steps 4 and 5 until queue is empty
4. Remove the front node N of Queue.
Process N and change the status of N to
the processed state (STATUS=3)
5. Add to the rear of queue all the
neighbors of N that are in ready state
(STATUS=1), and change their status to
waiting state (STATUS=2).
[End of Loop]
6. Exit
A
ED
CB
F
1.Initialize all nodes to the ready state
(STATUS=1)
1
1
1
1
1
1
2.Put the starting node A in Queue and
change its status to the waiting state
(STATUS=2)
A
2 1.Initialize all nodes to the ready state
(STATUS=1)
2.Put the starting node A in Queue and
change its status to the waiting state
(STATUS=2)
3. [LOOP]
Repeat steps 4 and 5 until queue is
empty
4. Remove the front node N of Queue.
Process N and change the status of N
to the processed state (STATUS=3)
5. Add to the rear of queue all the
neighbors of N that are in ready state
(STATUS=1), and change their status to
waiting state (STATUS=2).
[End of Loop]
6. Exit
3
B C E
2 2
2
D F
3
2
2
3
3
3
3
B
A
C E
D F
Traversing order – A B C E D F
Depth First Search (DFS)
* DFS uses stack
*During execution of algorithms , each node N of G will be
in one of three states called status of N:
- STATUS = 1(Ready state)
- STATUS = 2(Waiting state)
- STATUS= 3(Processed state)
Logic
A F
D
C E
B
1.Initialize all nodes to the ready state
(STATUS=1)
2.Push the starting node A onto stack
and change its status to the waiting state
(STATUS=2)
3. [LOOP]
Repeat steps 4 and 5 until stack is
empty
4. Pop the top node N of Stack.
Process N and change the status of N
to the processed state (STATUS=3)
5. Push onto Stack all the neighbors of
N that are still in ready state
(STATUS=1), and change their status to
waiting state (STATUS=2).
[End of Loop]
6. Exit
Algorithm
A F
D
C E
B
1.Initialize all nodes to the ready state
(STATUS=1)
1
1
11
1
1
2.Push the starting node A onto stack
and change its status to the waiting state
(STATUS=2)
A
2
A
1.Initialize all nodes to the ready state
(STATUS=1)
2.Push the starting node A onto stack
and change its status to the waiting state
(STATUS=2)
3. [LOOP]
Repeat steps 4 and 5 until stack is
empty
4. Pop the top node N of Stack.
Process N and change the status of N
to the processed state (STATUS=3)
5. Push onto Stack all the neighbors of
N that are still in ready state
(STATUS=1), and change their status to
waiting state (STATUS=2).
[End of Loop]
6. Exit
A
B
C
3
2
2
C
3
D
E
2
2
E
3
F
2
F
3
3 3
D B
Traversing order is
A C E F D B
A
C
E
F
D
B
Thank you
Jagjot singh chopra
Shivam khanna
2016 csa 1569 2016csa1556

More Related Content

What's hot (20)

Graphs in c language
Graphs in c languageGraphs in c language
Graphs in c language
 
Graph representation
Graph representationGraph representation
Graph representation
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Graph
GraphGraph
Graph
 
Skiena algorithm 2007 lecture10 graph data strctures
Skiena algorithm 2007 lecture10 graph data strcturesSkiena algorithm 2007 lecture10 graph data strctures
Skiena algorithm 2007 lecture10 graph data strctures
 
Graphs in data structure
Graphs in data structureGraphs in data structure
Graphs in data structure
 
Graph (Data structure)
Graph (Data structure) Graph (Data structure)
Graph (Data structure)
 
Graphss
GraphssGraphss
Graphss
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Graphs in Data Structure
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structure
 
Lecture8 data structure(graph)
Lecture8 data structure(graph)Lecture8 data structure(graph)
Lecture8 data structure(graph)
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data Structure
 
Graph
GraphGraph
Graph
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Graphs
GraphsGraphs
Graphs
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Graphs in Data Structure
Graphs in Data StructureGraphs in Data Structure
Graphs in Data Structure
 
Graph data structure
Graph data structureGraph data structure
Graph data structure
 
Graph Theory: Matrix representation of graphs
Graph Theory: Matrix representation of graphsGraph Theory: Matrix representation of graphs
Graph Theory: Matrix representation of graphs
 

Similar to Data structure

Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxAlgorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxzerihunnana
 
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
GRAPH - DISCRETE STRUCTURE AND ALGORITHMGRAPH - DISCRETE STRUCTURE AND ALGORITHM
GRAPH - DISCRETE STRUCTURE AND ALGORITHMhimanshumishra19dec
 
Topological sort
Topological sortTopological sort
Topological sortjabishah
 
Matrix representation of graph
Matrix representation of graphMatrix representation of graph
Matrix representation of graphRounak Biswas
 
Data Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph TraversalData Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph TraversalManishPrajapati78
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First SearchKevin Jadiya
 
topologicalsort-using c++ as development language.pptx
topologicalsort-using c++ as development language.pptxtopologicalsort-using c++ as development language.pptx
topologicalsort-using c++ as development language.pptxjanafridi251
 
Lecture 03 lexical analysis
Lecture 03 lexical analysisLecture 03 lexical analysis
Lecture 03 lexical analysisIffat Anjum
 
Elements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptxElements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptxmiki304759
 

Similar to Data structure (20)

Chap 8 graph
Chap 8 graphChap 8 graph
Chap 8 graph
 
Graphs
GraphsGraphs
Graphs
 
Graphs
GraphsGraphs
Graphs
 
6. Graphs
6. Graphs6. Graphs
6. Graphs
 
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxAlgorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
 
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
GRAPH - DISCRETE STRUCTURE AND ALGORITHMGRAPH - DISCRETE STRUCTURE AND ALGORITHM
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
 
Graphs
GraphsGraphs
Graphs
 
Topological sort
Topological sortTopological sort
Topological sort
 
Matrix representation of graph
Matrix representation of graphMatrix representation of graph
Matrix representation of graph
 
graph theory
graph theorygraph theory
graph theory
 
Data Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph TraversalData Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph Traversal
 
Graps 2
Graps 2Graps 2
Graps 2
 
Chap 6 Graph.ppt
Chap 6 Graph.pptChap 6 Graph.ppt
Chap 6 Graph.ppt
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Graph
GraphGraph
Graph
 
topologicalsort-using c++ as development language.pptx
topologicalsort-using c++ as development language.pptxtopologicalsort-using c++ as development language.pptx
topologicalsort-using c++ as development language.pptx
 
Lecture 03 lexical analysis
Lecture 03 lexical analysisLecture 03 lexical analysis
Lecture 03 lexical analysis
 
Graphs
GraphsGraphs
Graphs
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Elements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptxElements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptx
 

Recently uploaded

Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...kumargunjan9515
 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Klinik kandungan
 
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...gajnagarg
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteedamy56318795
 
Vastral Call Girls Book Now 7737669865 Top Class Escort Service Available
Vastral Call Girls Book Now 7737669865 Top Class Escort Service AvailableVastral Call Girls Book Now 7737669865 Top Class Escort Service Available
Vastral Call Girls Book Now 7737669865 Top Class Escort Service Availablegargpaaro
 
TrafficWave Generator Will Instantly drive targeted and engaging traffic back...
TrafficWave Generator Will Instantly drive targeted and engaging traffic back...TrafficWave Generator Will Instantly drive targeted and engaging traffic back...
TrafficWave Generator Will Instantly drive targeted and engaging traffic back...SOFTTECHHUB
 
💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...
💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...
💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...vershagrag
 
Oral Sex Call Girls Kashmiri Gate Delhi Just Call 👉👉 📞 8448380779 Top Class C...
Oral Sex Call Girls Kashmiri Gate Delhi Just Call 👉👉 📞 8448380779 Top Class C...Oral Sex Call Girls Kashmiri Gate Delhi Just Call 👉👉 📞 8448380779 Top Class C...
Oral Sex Call Girls Kashmiri Gate Delhi Just Call 👉👉 📞 8448380779 Top Class C...Delhi Call girls
 
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...nirzagarg
 
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...HyderabadDolls
 
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...nirzagarg
 
Giridih Escorts Service Girl ^ 9332606886, WhatsApp Anytime Giridih
Giridih Escorts Service Girl ^ 9332606886, WhatsApp Anytime GiridihGiridih Escorts Service Girl ^ 9332606886, WhatsApp Anytime Giridih
Giridih Escorts Service Girl ^ 9332606886, WhatsApp Anytime Giridihmeghakumariji156
 
Digital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareDigital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareGraham Ware
 
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...Elaine Werffeli
 
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi ArabiaIn Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabiaahmedjiabur940
 
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...gajnagarg
 
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...HyderabadDolls
 
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book nowVadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book nowgargpaaro
 
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...nirzagarg
 

Recently uploaded (20)

Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...
 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
 
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
 
Vastral Call Girls Book Now 7737669865 Top Class Escort Service Available
Vastral Call Girls Book Now 7737669865 Top Class Escort Service AvailableVastral Call Girls Book Now 7737669865 Top Class Escort Service Available
Vastral Call Girls Book Now 7737669865 Top Class Escort Service Available
 
TrafficWave Generator Will Instantly drive targeted and engaging traffic back...
TrafficWave Generator Will Instantly drive targeted and engaging traffic back...TrafficWave Generator Will Instantly drive targeted and engaging traffic back...
TrafficWave Generator Will Instantly drive targeted and engaging traffic back...
 
💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...
💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...
💞 Safe And Secure Call Girls Agra Call Girls Service Just Call 🍑👄6378878445 🍑...
 
Oral Sex Call Girls Kashmiri Gate Delhi Just Call 👉👉 📞 8448380779 Top Class C...
Oral Sex Call Girls Kashmiri Gate Delhi Just Call 👉👉 📞 8448380779 Top Class C...Oral Sex Call Girls Kashmiri Gate Delhi Just Call 👉👉 📞 8448380779 Top Class C...
Oral Sex Call Girls Kashmiri Gate Delhi Just Call 👉👉 📞 8448380779 Top Class C...
 
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
 
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
Sonagachi * best call girls in Kolkata | ₹,9500 Pay Cash 8005736733 Free Home...
 
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
 
Giridih Escorts Service Girl ^ 9332606886, WhatsApp Anytime Giridih
Giridih Escorts Service Girl ^ 9332606886, WhatsApp Anytime GiridihGiridih Escorts Service Girl ^ 9332606886, WhatsApp Anytime Giridih
Giridih Escorts Service Girl ^ 9332606886, WhatsApp Anytime Giridih
 
Digital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareDigital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham Ware
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
 
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
 
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi ArabiaIn Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
 
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
 
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
 
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book nowVadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
 
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
 

Data structure

  • 2. What is a Graph?  A data Structure that consists of a set of nodes(vertices) and a set of edges that relate the node to each other.  The set of edges describes relationship among the vertices. edge Node
  • 3. Formal Definition Of Graphs  A graph G is defined as follows: G=(V,E) V: set of vertices E:set of edges connecting the vertices in V An edge e=(u , v) is a pair of vertices Example: a b c d e V={a,bc,d,e} E={(a,b),(a,c),(a,d),(b,e) ,(c,d),(c,e),(d,e)}
  • 4.  When the edges in a graph have no direction , the graph is called undirected.  When the edges in a graph have a direction , the graph is called directed. Undirected edge Directed edge a b a b 1 2 5 4 3 1 2 5 4 3 Undirected Graph Directed Graph
  • 5.  Adjacent nodes : two nodes are adjacent if they are connected by an edge.  Path : a sequence of vertices that connect two nodes in a graph  Simple path : A simple path is a path in which all vertices , except possibly in the first and last ,are different.  Complete Graph: a graph in which every vertex is directly connected to every other vertex a b A is adjacent to b
  • 6.  Adjacency matrix representation.  Adjacency lists representation.
  • 7. Adjacency matrix Let G=(V , E) be a graph with n vertices. The adjacency matrix of G is a two dimensional array n by n , say A[5][5]. A B C D E (A) 1 (B) 2 (C) 3 (D)4 (E)5 (A)1 (B)2 (C)3 (D)4 (E)5 0 1 1 1 0 01 0 1 0 1 0 0 1 1 1 1 1 0 1 0 0 1 1 0 When the adjacent vertices of a graph is represented in the form of matrix then such type of representation is called Adjacency Matrix Representation of graph. E.g. A[i][j] Number of rows = Number of Column A ij = O , if Vi is not adjacent to Vj 1 , if Vi , Vj is adjacent
  • 8. Adjacency lists B C E A D NODE ADJACENCY LIST A B , C , D B C C NULL D C , E E C When the vertices of graph are represented by the linked list then such type of representation is called adjacency list representation of graph.
  • 9. Traverse all node in the graph. Methods * Breadth – First Search *Depth - First Search
  • 10. Breadth First Search(BFS) *BFS uses queue. Logic *During execution of algorithms , each node N of G will be in one of three states called status of N: - STATUS = 1(Ready state) - STATUS = 2(Waiting state) - STATUS= 3(Processed state)
  • 11. ED C F A B Algorithm 1.Initialize all nodes to the ready state (STATUS=1) 2.Put the starting node A in Queue and change its status to the waiting state (STATUS=2) 3. [LOOP] Repeat steps 4 and 5 until queue is empty 4. Remove the front node N of Queue. Process N and change the status of N to the processed state (STATUS=3) 5. Add to the rear of queue all the neighbors of N that are in ready state (STATUS=1), and change their status to waiting state (STATUS=2). [End of Loop] 6. Exit
  • 12. A ED CB F 1.Initialize all nodes to the ready state (STATUS=1) 1 1 1 1 1 1 2.Put the starting node A in Queue and change its status to the waiting state (STATUS=2) A 2 1.Initialize all nodes to the ready state (STATUS=1) 2.Put the starting node A in Queue and change its status to the waiting state (STATUS=2) 3. [LOOP] Repeat steps 4 and 5 until queue is empty 4. Remove the front node N of Queue. Process N and change the status of N to the processed state (STATUS=3) 5. Add to the rear of queue all the neighbors of N that are in ready state (STATUS=1), and change their status to waiting state (STATUS=2). [End of Loop] 6. Exit 3 B C E 2 2 2 D F 3 2 2 3 3 3 3 B A C E D F Traversing order – A B C E D F
  • 13. Depth First Search (DFS) * DFS uses stack *During execution of algorithms , each node N of G will be in one of three states called status of N: - STATUS = 1(Ready state) - STATUS = 2(Waiting state) - STATUS= 3(Processed state) Logic
  • 14. A F D C E B 1.Initialize all nodes to the ready state (STATUS=1) 2.Push the starting node A onto stack and change its status to the waiting state (STATUS=2) 3. [LOOP] Repeat steps 4 and 5 until stack is empty 4. Pop the top node N of Stack. Process N and change the status of N to the processed state (STATUS=3) 5. Push onto Stack all the neighbors of N that are still in ready state (STATUS=1), and change their status to waiting state (STATUS=2). [End of Loop] 6. Exit Algorithm
  • 15. A F D C E B 1.Initialize all nodes to the ready state (STATUS=1) 1 1 11 1 1 2.Push the starting node A onto stack and change its status to the waiting state (STATUS=2) A 2 A 1.Initialize all nodes to the ready state (STATUS=1) 2.Push the starting node A onto stack and change its status to the waiting state (STATUS=2) 3. [LOOP] Repeat steps 4 and 5 until stack is empty 4. Pop the top node N of Stack. Process N and change the status of N to the processed state (STATUS=3) 5. Push onto Stack all the neighbors of N that are still in ready state (STATUS=1), and change their status to waiting state (STATUS=2). [End of Loop] 6. Exit A B C 3 2 2 C 3 D E 2 2 E 3 F 2 F 3 3 3 D B
  • 16. Traversing order is A C E F D B A C E F D B
  • 17. Thank you Jagjot singh chopra Shivam khanna 2016 csa 1569 2016csa1556