SlideShare une entreprise Scribd logo
1  sur  31
GRAPH
PRESENTED BY
R.RAMADEVI
M.sc(CS & IT)
NADAR SARASWATHI COLLEGE OF ARTS & SCIENCE
THENI
GRAPH TRAVERSALS
 • Depth-First Search
 • Breadth-First Search
Depth-first search (DFS)
• A depth-first search (DFS) in an undirected graph G is like
wandering in a labyrinth with a string and a can of red paint without
getting lost.
• We start at vertex s, tying the end of our string to the point and
paintings “visited”. Next we label s as our current vertex called u.
• Now we travel along an arbitrary edge (u,v)
.If edge (u,v) leads us to an already visited vertex v we return to u.
Depth-first search (DFS)
1. If vertex v is unvisited, we unroll our string and move to v, paint v
“visited”, set v as our current vertex, and repeat the previous steps.
2.Eventually, we will get to a point where all incident edges on u lead
to visited vertices.
3.We then backtrack by unrolling our string to a previously visited
vertex v.
4.Then v becomes our current vertex and we repeat the previous steps
Depth-First Search
 Algorithm DFS(v);
 Input: A vertex v in a graph
 Output: A labeling of the edges as “discovery” edges
and “backedges”
for each edge e incident on v do
if edge e is unexplored then
let w be the other endpoint of e
if vertex w is unexplored then
label e as a discovery edge
recursively call DFS(w)
else
label e as a backedge
Depth First Search
Depth First Search
Generic Depth First Search
public abstract class DFS {
protected Object dfsVisit(Vertex v) {
protected InspectableGraph graph;
protected Object visitResult;
initResult();
startVisit(v);
mark(v);
for (Enumeration inEdges = graph.incidentEdges(v);
inEdges.hasMoreElements();) {
Edge nextEdge = (Edge) inEdges.nextElement();
if (!isMarked(nextEdge)) { // found an unexplored edge
mark(nextEdge);
Vertex w = graph.opposite(v, nextEdge);
if (!isMarked(w)) { // discovery edge
mark(nextEdge);
traverseDiscovery(nextEdge, v);
if (!isDone())
VisitResult = dfVisit(w); }
else // back edge
TraverseBack(nextEdge, v);
}
}
FinishVisit(v);
return result();
}
Breadth-First Search
 Like DFS, a Breadth-First Search (BFS) traverses a connected
component of a graph, and in doing so defines a spanning tree with
several useful properties
 The starting vertex s has level 0, and, as in DFS , defines that point as
an “anchor.”
 In the first round, the string is unrolled the length of one edge, and all of
the edges that are only one edge away from the anchor are visited.
 These edges are placed into level 1
Breadth-First Search
 In the second round, all the new edges that can be reached by
unrolling the string 2 edges are visited and placed in level 2.
 This continues until every vertex has been assigned a level.
 The label of any vertex v corresponds to the length of the shortest
path from s to v.
BFS - A Graphical Representation
More BFS
BFS Pseudo-Code
Algorithm BFS(s):
Input: A vertex s in a graph
Output: A labeling of the edges as “discovery” edges
and “cross edges”
initialize container L0 to contain vertex s
i ¬ 0
while Li is not empty do
create container Li+1 to initially be empty
for each vertex v in Li do
if edge e incident on v do
let w be the other endpoint of e
if vertex w is unexplored then
label e as a discovery edge
insert w into Li+1
else
label e as a cross edge
i ¬ i + 1
Spanning tree
A spanning tree of a graph is just a subgraph that contains all the vertices and is a
tree.
A graph may have many spanning trees.
Minimum Spanning Trees
 The Minimum Spanning Tree for a given graph is the Spanning Tree
of minimum cost for that graph.
Algorithms for Obtaining the Minimum
Spanning Tree
 Kruskal's Algorithm
 Prim's Algorithm
 Boruvka's Algorithm
Kruskal's Algorithm
 This algorithm creates a forest of trees. Initially the forest consists of n single
node trees (and no edges).
 At each step, we add one edge (the cheapest one) so that it joins two trees
together.
 If it were to form a cycle, it would simply link two nodes that were already part of
a single connected tree, so that this edge would not be needed.
Complete graph
Prim's Algorithm
 This algorithm starts with one node.
 It then, one by one, adds a node that is unconnected to the new
graph to the new graph, each time selecting the node.
 whose connecting edge has the smallest weight out of the available
nodes’ connecting edges.
Boruvka's Algorithm
 This algorithm is similar to Prim’s, but nodes are added to the new
graph in parallel all around the graph.
 It creates a list of trees, each containing one node from the original
graph and proceeds to merge them along the smallest-weight
connecting edges until there’s only one tree, which is, of course, the
MST.
 It works rather like a merge sort.
THANKYOU!!!!!

Contenu connexe

Tendances

Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphsKumar
 
2.1 graph basic
2.1 graph basic 2.1 graph basic
2.1 graph basic Krish_ver2
 
Graph representation
Graph representationGraph representation
Graph representationTech_MX
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structureAbrish06
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structuresSavit Chandra
 
Application Of Graph Data Structure
Application Of Graph Data StructureApplication Of Graph Data Structure
Application Of Graph Data StructureGaurang Dobariya
 
Data Structure Graph DMZ #DMZone
Data Structure Graph DMZ #DMZoneData Structure Graph DMZ #DMZone
Data Structure Graph DMZ #DMZoneDoug Needham
 
Graphs in data structure
Graphs in data structureGraphs in data structure
Graphs in data structurehamza javed
 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structureMahmoud Alfarra
 
Graphs in c language
Graphs in c languageGraphs in c language
Graphs in c languageSARITHA REDDY
 
Data Structure : Graph and Graph Traversing
Data Structure : Graph and Graph TraversingData Structure : Graph and Graph Traversing
Data Structure : Graph and Graph TraversingVikas Chandwani
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsYi-Lung Tsai
 
Matrix representation of graph
Matrix representation of graphMatrix representation of graph
Matrix representation of graphRounak Biswas
 
Graph Theory: Matrix representation of graphs
Graph Theory: Matrix representation of graphsGraph Theory: Matrix representation of graphs
Graph Theory: Matrix representation of graphsAshikur Rahman
 
Data structures and algorithms lab7
Data structures and algorithms lab7Data structures and algorithms lab7
Data structures and algorithms lab7Bianca Teşilă
 

Tendances (20)

Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphs
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
2.1 graph basic
2.1 graph basic 2.1 graph basic
2.1 graph basic
 
Graph representation
Graph representationGraph representation
Graph representation
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
Graphss
GraphssGraphss
Graphss
 
Application Of Graph Data Structure
Application Of Graph Data StructureApplication Of Graph Data Structure
Application Of Graph Data Structure
 
Graphs data Structure
Graphs data StructureGraphs data Structure
Graphs data Structure
 
Data Structure Graph DMZ #DMZone
Data Structure Graph DMZ #DMZoneData Structure Graph DMZ #DMZone
Data Structure Graph DMZ #DMZone
 
Graphs in data structure
Graphs in data structureGraphs in data structure
Graphs in data structure
 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structure
 
Graphs in c language
Graphs in c languageGraphs in c language
Graphs in c language
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Data Structure : Graph and Graph Traversing
Data Structure : Graph and Graph TraversingData Structure : Graph and Graph Traversing
Data Structure : Graph and Graph Traversing
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
 
Graph
GraphGraph
Graph
 
Matrix representation of graph
Matrix representation of graphMatrix representation of graph
Matrix representation of graph
 
Graph Theory: Matrix representation of graphs
Graph Theory: Matrix representation of graphsGraph Theory: Matrix representation of graphs
Graph Theory: Matrix representation of graphs
 
Data structures and algorithms lab7
Data structures and algorithms lab7Data structures and algorithms lab7
Data structures and algorithms lab7
 

Similaire à Graphs in Data Structure

Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalAmrinder Arora
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfChristianKapsales1
 
Skiena algorithm 2007 lecture11 breadth deapth first search
Skiena algorithm 2007 lecture11 breadth deapth first searchSkiena algorithm 2007 lecture11 breadth deapth first search
Skiena algorithm 2007 lecture11 breadth deapth first searchzukun
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx pptDhruvilSTATUS
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashingVictor Palmar
 
Social network analysis
Social network analysisSocial network analysis
Social network analysisStefanie Zhao
 
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
 
Graph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptxGraph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptxsahilpawar2426
 
graph representation.pdf
graph representation.pdfgraph representation.pdf
graph representation.pdfamitbhachne
 
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)Mohanlal Sukhadia University (MLSU)
 
Biconnected components (13024116056)
Biconnected components (13024116056)Biconnected components (13024116056)
Biconnected components (13024116056)Akshay soni
 

Similaire à Graphs in Data Structure (20)

Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search Traversal
 
Graphs
GraphsGraphs
Graphs
 
DATA STRUCTURES.pptx
DATA STRUCTURES.pptxDATA STRUCTURES.pptx
DATA STRUCTURES.pptx
 
logic.pptx
logic.pptxlogic.pptx
logic.pptx
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
 
Skiena algorithm 2007 lecture11 breadth deapth first search
Skiena algorithm 2007 lecture11 breadth deapth first searchSkiena algorithm 2007 lecture11 breadth deapth first search
Skiena algorithm 2007 lecture11 breadth deapth first search
 
U1 L5 DAA.pdf
U1 L5 DAA.pdfU1 L5 DAA.pdf
U1 L5 DAA.pdf
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 
Social network analysis
Social network analysisSocial network analysis
Social network analysis
 
NON-LINEAR DATA STRUCTURE-Graphs.pptx
NON-LINEAR DATA STRUCTURE-Graphs.pptxNON-LINEAR DATA STRUCTURE-Graphs.pptx
NON-LINEAR DATA STRUCTURE-Graphs.pptx
 
Graph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptxGraph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptx
 
Data structure note
Data structure noteData structure note
Data structure note
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
graph representation.pdf
graph representation.pdfgraph representation.pdf
graph representation.pdf
 
Weighted graphs
Weighted graphsWeighted graphs
Weighted graphs
 
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
 
Graphs
GraphsGraphs
Graphs
 
Biconnected components (13024116056)
Biconnected components (13024116056)Biconnected components (13024116056)
Biconnected components (13024116056)
 

Plus de lalithambiga kamaraj (20)

Firewall in Network Security
Firewall in Network SecurityFirewall in Network Security
Firewall in Network Security
 
Data Compression in Multimedia
Data Compression in MultimediaData Compression in Multimedia
Data Compression in Multimedia
 
Data CompressionMultimedia
Data CompressionMultimediaData CompressionMultimedia
Data CompressionMultimedia
 
Digital Audio in Multimedia
Digital Audio in MultimediaDigital Audio in Multimedia
Digital Audio in Multimedia
 
Network Security: Physical security
Network Security: Physical security Network Security: Physical security
Network Security: Physical security
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Data structure
Data structureData structure
Data structure
 
Digital Image Processing
Digital Image ProcessingDigital Image Processing
Digital Image Processing
 
Digital Image Processing
Digital Image ProcessingDigital Image Processing
Digital Image Processing
 
Estimating Software Maintenance Costs
Estimating Software Maintenance CostsEstimating Software Maintenance Costs
Estimating Software Maintenance Costs
 
Datamining
DataminingDatamining
Datamining
 
Digital Components
Digital ComponentsDigital Components
Digital Components
 
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating system
 
Io management disk scheduling algorithm
Io management disk scheduling algorithmIo management disk scheduling algorithm
Io management disk scheduling algorithm
 
Recovery system
Recovery systemRecovery system
Recovery system
 
File management
File managementFile management
File management
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Inheritance
InheritanceInheritance
Inheritance
 
Managing console of I/o operations & working with files
Managing console of I/o operations & working with filesManaging console of I/o operations & working with files
Managing console of I/o operations & working with files
 

Dernier

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 

Dernier (20)

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 

Graphs in Data Structure

  • 1. GRAPH PRESENTED BY R.RAMADEVI M.sc(CS & IT) NADAR SARASWATHI COLLEGE OF ARTS & SCIENCE THENI
  • 2. GRAPH TRAVERSALS  • Depth-First Search  • Breadth-First Search
  • 3. Depth-first search (DFS) • A depth-first search (DFS) in an undirected graph G is like wandering in a labyrinth with a string and a can of red paint without getting lost. • We start at vertex s, tying the end of our string to the point and paintings “visited”. Next we label s as our current vertex called u. • Now we travel along an arbitrary edge (u,v) .If edge (u,v) leads us to an already visited vertex v we return to u.
  • 4. Depth-first search (DFS) 1. If vertex v is unvisited, we unroll our string and move to v, paint v “visited”, set v as our current vertex, and repeat the previous steps. 2.Eventually, we will get to a point where all incident edges on u lead to visited vertices. 3.We then backtrack by unrolling our string to a previously visited vertex v. 4.Then v becomes our current vertex and we repeat the previous steps
  • 5. Depth-First Search  Algorithm DFS(v);  Input: A vertex v in a graph  Output: A labeling of the edges as “discovery” edges and “backedges” for each edge e incident on v do if edge e is unexplored then let w be the other endpoint of e if vertex w is unexplored then label e as a discovery edge recursively call DFS(w) else label e as a backedge
  • 8. Generic Depth First Search public abstract class DFS { protected Object dfsVisit(Vertex v) { protected InspectableGraph graph; protected Object visitResult; initResult(); startVisit(v); mark(v); for (Enumeration inEdges = graph.incidentEdges(v); inEdges.hasMoreElements();) { Edge nextEdge = (Edge) inEdges.nextElement(); if (!isMarked(nextEdge)) { // found an unexplored edge
  • 9. mark(nextEdge); Vertex w = graph.opposite(v, nextEdge); if (!isMarked(w)) { // discovery edge mark(nextEdge); traverseDiscovery(nextEdge, v); if (!isDone()) VisitResult = dfVisit(w); } else // back edge TraverseBack(nextEdge, v); } } FinishVisit(v); return result(); }
  • 10. Breadth-First Search  Like DFS, a Breadth-First Search (BFS) traverses a connected component of a graph, and in doing so defines a spanning tree with several useful properties  The starting vertex s has level 0, and, as in DFS , defines that point as an “anchor.”  In the first round, the string is unrolled the length of one edge, and all of the edges that are only one edge away from the anchor are visited.  These edges are placed into level 1
  • 11. Breadth-First Search  In the second round, all the new edges that can be reached by unrolling the string 2 edges are visited and placed in level 2.  This continues until every vertex has been assigned a level.  The label of any vertex v corresponds to the length of the shortest path from s to v.
  • 12. BFS - A Graphical Representation
  • 14. BFS Pseudo-Code Algorithm BFS(s): Input: A vertex s in a graph Output: A labeling of the edges as “discovery” edges and “cross edges” initialize container L0 to contain vertex s i ¬ 0 while Li is not empty do create container Li+1 to initially be empty for each vertex v in Li do if edge e incident on v do let w be the other endpoint of e if vertex w is unexplored then label e as a discovery edge insert w into Li+1 else label e as a cross edge i ¬ i + 1
  • 15. Spanning tree A spanning tree of a graph is just a subgraph that contains all the vertices and is a tree. A graph may have many spanning trees.
  • 16.
  • 17. Minimum Spanning Trees  The Minimum Spanning Tree for a given graph is the Spanning Tree of minimum cost for that graph.
  • 18. Algorithms for Obtaining the Minimum Spanning Tree  Kruskal's Algorithm  Prim's Algorithm  Boruvka's Algorithm
  • 19. Kruskal's Algorithm  This algorithm creates a forest of trees. Initially the forest consists of n single node trees (and no edges).  At each step, we add one edge (the cheapest one) so that it joins two trees together.  If it were to form a cycle, it would simply link two nodes that were already part of a single connected tree, so that this edge would not be needed.
  • 20.
  • 22. Prim's Algorithm  This algorithm starts with one node.  It then, one by one, adds a node that is unconnected to the new graph to the new graph, each time selecting the node.  whose connecting edge has the smallest weight out of the available nodes’ connecting edges.
  • 23.
  • 24.
  • 25. Boruvka's Algorithm  This algorithm is similar to Prim’s, but nodes are added to the new graph in parallel all around the graph.  It creates a list of trees, each containing one node from the original graph and proceeds to merge them along the smallest-weight connecting edges until there’s only one tree, which is, of course, the MST.  It works rather like a merge sort.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.