SlideShare une entreprise Scribd logo
1  sur  32
Graph Data Structure
Prepared by: Afaq Mansoor Khan
BSSE III- Group A
Session 2017-21
IMSciences, Peshawar.
Last Lecture Summary
• Introduction to AVL tree
• Insertion and deletion of a node in AVL tree.
• Balancing of Tree
• Calculating Height of a particular node in the Tree
• Calculating balance Factor for a particular Node in
the Tree
• Left Rotation in an AVL Tree
• Right Rotation in an AVL Tree
• All types of Traversals in AVL
• Algorithm Analysis
Objectives Overview
• Introduction to Graph Data Structure.
• Depth First Traversal In Graph
• Breadth First Traversal in Graph
Graph Data Structure
• A Graph is a non-linear data structure consisting of
nodes and edges. The nodes are sometimes also
referred to as vertices and the edges are lines or arcs
that connect any two nodes in the graph. More
formally a Graph can be defined as:
▫ A Graph consists of a finite set of vertices(or nodes)
and set of Edges which connect a pair of nodes.
In the above Graph, the set of vertices V = {0,1,2,3,4} and the set of
edges E = {01, 12, 23, 34, 04, 14, 13}.
Directed vs. undirected graphs
• When the edges in a graph have no direction, the
graph is called undirected
Directed vs. undirected graphs
• When the edges in a graph have a direction, the
graph is called directed (or digraph)
E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7)
Warning: if the graph is
directed, the order of the
vertices in each edge is
important !!
Trees vs graphs
• Trees are special cases of graphs!!
Graph terminology
• 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
• Complete graph: a graph in which every vertex is
directly connected to every other vertex
5 is adjacent to 7
7 is adjacent from 5
Graph terminology
• What is the number of edges in a complete
directed graph with N vertices?
N * (N-1)
2
( )O N
Graph terminology
• What is the number of edges in a complete
undirected graph with N vertices?
N * (N-1) / 2
2
( )O N
Graph terminology
• Weighted graph: a graph in which each edge
carries a value
Graph implementation
• Array-based implementation
▫ A 1D array is used to represent the vertices
▫ A 2D array (adjacency matrix) is used to represent
the edges
Array-based implementation
Graph implementation
• Linked-list implementation
▫ A 1D array is used to represent the vertices
▫ A list is used for each vertex v which contains the
vertices which are adjacent from v (adjacency list)
Linked-list implementation
Adjacency matrix vs. adjacency list
representation
• Adjacency matrix
▫ Good for dense graphs --|E|~O(|V|2)
▫ Memory requirements: O(|V| + |E| ) = O(|V|2 )
▫ Connectivity between two vertices can be tested
quickly
• Adjacency list
▫ Good for sparse graphs -- |E|~O(|V|)
▫ Memory requirements: O(|V| + |E|)=O(|V|)
▫ Vertices adjacent to another vertex can be found
quickly
Graph searching
• Problem: find a path between two nodes of the
graph (e.g., Austin and Washington)
• Methods: Depth-First-Search (DFS) or Breadth-
First-Search (BFS)
Depth-First-Search (DFS)
• What is the idea behind DFS?
▫ Travel as far as you can down a path
▫ Back up as little as possible when you reach a "dead
end" (i.e., next vertex has been "marked" or there is
no next vertex)
• DFS can be implemented efficiently using a stack
Depth-First-Search (DFS)
1. Set found to false
2. stack.Push(startVertex)
3. DO
4. stack.Pop(vertex)
5. IF vertex == endVertex
6. Set found to true
7. ELSE
8. Push all adjacent vertices onto stack
9. WHILE !stack.IsEmpty() AND !found
10.
11. IF(!found)
12. Write "Path does not exist"
start end
(initialization)
Breadth-First-Searching (BFS)
• What is the idea behind BFS?
▫ Look at all possible paths at the same depth before
you go at a deeper level
▫ Back up as far as possible when you reach a "dead
end" (i.e., next vertex has been "marked" or there
is no next vertex)
Breadth-First-Searching (BFS)
• BFS can be implemented efficiently using a queue
Set found to false
queue.Enqueue(startVertex)
DO
queue.Dequeue(vertex)
IF vertex == endVertex
Set found to true
ELSE
Enqueue all adjacent vertices onto queue
WHILE !queue.IsEmpty() AND !found
• Should we mark a vertex when it is enqueued or
when it is dequeued ?
IF(!found)
Write "Path does not exist"
start end
(initialization)
next:
Single-source shortest-path problem
• There are multiple paths from a source vertex to a
destination vertex
• Shortest path: the path whose total weight (i.e.,
sum of edge weights) is minimum
• Examples:
▫ Austin->Houston->Atlanta->Washington: 1560
miles
▫ Austin->Dallas->Denver->Atlanta->Washington: 2980
miles
Single-source shortest-path problem
• Common algorithms: Dijkstra's algorithm,
Bellman-Ford algorithm
• BFS can be used to solve the shortest graph
problem when the graph is weightless or all the
weights are the same
(mark vertices before Enqueue)
Summary
• Introduction to Graph Data Structure
▫ Terminology
▫ Implementation
▫ Directed graphs
▫ Undirected graphs
• Depth First Traversal In Graph
• Breadth First Traversal in Graph
References
• https://www.geeksforgeeks.org/graph-data-
structure-and-algorithms/
• www.csie.ntu.edu.tw/~ds/ppt/ch6/chapter6.PPT
• https://www.tutorialspoint.com/data_structures_alg
orithms/graph_data_structure.htm
• https://www.programiz.com/dsa/graph
• https://www.cse.unr.edu/~bebis/CS308/PowerPoint/
Graphs.ppt

Contenu connexe

Tendances

My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
Senthil Kumar
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
Sajid Marwat
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphs
Kumar
 

Tendances (20)

Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Threaded binary tree
Threaded binary treeThreaded binary tree
Threaded binary tree
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
 
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
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
Red black tree
Red black treeRed black tree
Red black tree
 
Dijkstra
DijkstraDijkstra
Dijkstra
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Directed Acyclic Graph
Directed Acyclic Graph Directed Acyclic Graph
Directed Acyclic Graph
 
Graph data structure and algorithms
Graph data structure and algorithmsGraph data structure and algorithms
Graph data structure and algorithms
 
single linked list
single linked listsingle linked list
single linked list
 
Trees and graphs
Trees and graphsTrees and graphs
Trees and graphs
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 
Stack
StackStack
Stack
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Graph coloring Algorithm
Graph coloring AlgorithmGraph coloring Algorithm
Graph coloring Algorithm
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphs
 

Similaire à Graph Data Structure

Similaire à Graph Data Structure (20)

Graphs
GraphsGraphs
Graphs
 
Graphs (1)
Graphs (1)Graphs (1)
Graphs (1)
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Graphs.ppt
Graphs.pptGraphs.ppt
Graphs.ppt
 
Graphs.ppt of mathemaics we have to clar all doubts
Graphs.ppt of mathemaics  we have to clar all doubtsGraphs.ppt of mathemaics  we have to clar all doubts
Graphs.ppt of mathemaics we have to clar all doubts
 
Unit V - ppt.pptx
Unit V - ppt.pptxUnit V - ppt.pptx
Unit V - ppt.pptx
 
UNIT III.pptx
UNIT III.pptxUNIT III.pptx
UNIT III.pptx
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Graphs
GraphsGraphs
Graphs
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
 
ppt 1.pptx
ppt 1.pptxppt 1.pptx
ppt 1.pptx
 
Graphs
GraphsGraphs
Graphs
 
Graphs
GraphsGraphs
Graphs
 
Unit VI - Graphs.ppt
Unit VI - Graphs.pptUnit VI - Graphs.ppt
Unit VI - Graphs.ppt
 
U1 L5 DAA.pdf
U1 L5 DAA.pdfU1 L5 DAA.pdf
U1 L5 DAA.pdf
 
Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptx
 
Graph Algorithms
Graph AlgorithmsGraph Algorithms
Graph Algorithms
 
Graphs data structures
Graphs data structuresGraphs data structures
Graphs data structures
 

Plus de Afaq Mansoor Khan

Plus de Afaq Mansoor Khan (20)

Feature Selection - Natural Language Processing
Feature Selection - Natural Language ProcessingFeature Selection - Natural Language Processing
Feature Selection - Natural Language Processing
 
WiFi vs LiFi - A Comparison
WiFi vs LiFi - A ComparisonWiFi vs LiFi - A Comparison
WiFi vs LiFi - A Comparison
 
Role of Electronic Media in Pakistan
Role of Electronic Media in PakistanRole of Electronic Media in Pakistan
Role of Electronic Media in Pakistan
 
Agile Testing - Approach and Strategies
Agile Testing - Approach and StrategiesAgile Testing - Approach and Strategies
Agile Testing - Approach and Strategies
 
Ethical Hacking - An Overview
Ethical Hacking - An OverviewEthical Hacking - An Overview
Ethical Hacking - An Overview
 
Software Architecture Design Decisions
Software Architecture Design DecisionsSoftware Architecture Design Decisions
Software Architecture Design Decisions
 
How to Design an Algorithm
How to Design an AlgorithmHow to Design an Algorithm
How to Design an Algorithm
 
Software Quality Qssurance, Scrum and Linkedin
Software Quality Qssurance, Scrum and LinkedinSoftware Quality Qssurance, Scrum and Linkedin
Software Quality Qssurance, Scrum and Linkedin
 
Quick sort
Quick sortQuick sort
Quick sort
 
.Physics presentation - Asteroids
.Physics presentation - Asteroids.Physics presentation - Asteroids
.Physics presentation - Asteroids
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 
Binary tree
Binary treeBinary tree
Binary tree
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked Lists
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Recursion and Sorting Algorithms
Recursion and Sorting AlgorithmsRecursion and Sorting Algorithms
Recursion and Sorting Algorithms
 

Dernier

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
anilsa9823
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Dernier (20)

SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 

Graph Data Structure

  • 1. Graph Data Structure Prepared by: Afaq Mansoor Khan BSSE III- Group A Session 2017-21 IMSciences, Peshawar.
  • 2. Last Lecture Summary • Introduction to AVL tree • Insertion and deletion of a node in AVL tree. • Balancing of Tree • Calculating Height of a particular node in the Tree • Calculating balance Factor for a particular Node in the Tree • Left Rotation in an AVL Tree • Right Rotation in an AVL Tree • All types of Traversals in AVL • Algorithm Analysis
  • 3. Objectives Overview • Introduction to Graph Data Structure. • Depth First Traversal In Graph • Breadth First Traversal in Graph
  • 4. Graph Data Structure • A Graph is a non-linear data structure consisting of nodes and edges. The nodes are sometimes also referred to as vertices and the edges are lines or arcs that connect any two nodes in the graph. More formally a Graph can be defined as: ▫ A Graph consists of a finite set of vertices(or nodes) and set of Edges which connect a pair of nodes.
  • 5. In the above Graph, the set of vertices V = {0,1,2,3,4} and the set of edges E = {01, 12, 23, 34, 04, 14, 13}.
  • 6. Directed vs. undirected graphs • When the edges in a graph have no direction, the graph is called undirected
  • 7. Directed vs. undirected graphs • When the edges in a graph have a direction, the graph is called directed (or digraph) E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7) Warning: if the graph is directed, the order of the vertices in each edge is important !!
  • 8. Trees vs graphs • Trees are special cases of graphs!!
  • 9. Graph terminology • 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 • Complete graph: a graph in which every vertex is directly connected to every other vertex 5 is adjacent to 7 7 is adjacent from 5
  • 10. Graph terminology • What is the number of edges in a complete directed graph with N vertices? N * (N-1) 2 ( )O N
  • 11. Graph terminology • What is the number of edges in a complete undirected graph with N vertices? N * (N-1) / 2 2 ( )O N
  • 12. Graph terminology • Weighted graph: a graph in which each edge carries a value
  • 13. Graph implementation • Array-based implementation ▫ A 1D array is used to represent the vertices ▫ A 2D array (adjacency matrix) is used to represent the edges
  • 15. Graph implementation • Linked-list implementation ▫ A 1D array is used to represent the vertices ▫ A list is used for each vertex v which contains the vertices which are adjacent from v (adjacency list)
  • 17. Adjacency matrix vs. adjacency list representation • Adjacency matrix ▫ Good for dense graphs --|E|~O(|V|2) ▫ Memory requirements: O(|V| + |E| ) = O(|V|2 ) ▫ Connectivity between two vertices can be tested quickly • Adjacency list ▫ Good for sparse graphs -- |E|~O(|V|) ▫ Memory requirements: O(|V| + |E|)=O(|V|) ▫ Vertices adjacent to another vertex can be found quickly
  • 18. Graph searching • Problem: find a path between two nodes of the graph (e.g., Austin and Washington) • Methods: Depth-First-Search (DFS) or Breadth- First-Search (BFS)
  • 19. Depth-First-Search (DFS) • What is the idea behind DFS? ▫ Travel as far as you can down a path ▫ Back up as little as possible when you reach a "dead end" (i.e., next vertex has been "marked" or there is no next vertex) • DFS can be implemented efficiently using a stack
  • 20. Depth-First-Search (DFS) 1. Set found to false 2. stack.Push(startVertex) 3. DO 4. stack.Pop(vertex) 5. IF vertex == endVertex 6. Set found to true 7. ELSE 8. Push all adjacent vertices onto stack 9. WHILE !stack.IsEmpty() AND !found 10. 11. IF(!found) 12. Write "Path does not exist"
  • 22.
  • 23.
  • 24. Breadth-First-Searching (BFS) • What is the idea behind BFS? ▫ Look at all possible paths at the same depth before you go at a deeper level ▫ Back up as far as possible when you reach a "dead end" (i.e., next vertex has been "marked" or there is no next vertex)
  • 25. Breadth-First-Searching (BFS) • BFS can be implemented efficiently using a queue Set found to false queue.Enqueue(startVertex) DO queue.Dequeue(vertex) IF vertex == endVertex Set found to true ELSE Enqueue all adjacent vertices onto queue WHILE !queue.IsEmpty() AND !found • Should we mark a vertex when it is enqueued or when it is dequeued ? IF(!found) Write "Path does not exist"
  • 27. next:
  • 28.
  • 29. Single-source shortest-path problem • There are multiple paths from a source vertex to a destination vertex • Shortest path: the path whose total weight (i.e., sum of edge weights) is minimum • Examples: ▫ Austin->Houston->Atlanta->Washington: 1560 miles ▫ Austin->Dallas->Denver->Atlanta->Washington: 2980 miles
  • 30. Single-source shortest-path problem • Common algorithms: Dijkstra's algorithm, Bellman-Ford algorithm • BFS can be used to solve the shortest graph problem when the graph is weightless or all the weights are the same (mark vertices before Enqueue)
  • 31. Summary • Introduction to Graph Data Structure ▫ Terminology ▫ Implementation ▫ Directed graphs ▫ Undirected graphs • Depth First Traversal In Graph • Breadth First Traversal in Graph
  • 32. References • https://www.geeksforgeeks.org/graph-data- structure-and-algorithms/ • www.csie.ntu.edu.tw/~ds/ppt/ch6/chapter6.PPT • https://www.tutorialspoint.com/data_structures_alg orithms/graph_data_structure.htm • https://www.programiz.com/dsa/graph • https://www.cse.unr.edu/~bebis/CS308/PowerPoint/ Graphs.ppt