Graph Data Structure

K
Keno bentiLecturer and Software Developer
HARAMAYA UNIVERSITY
SCHOOL OF GRADUATE STUDIES
DEPARTMENT OF COMPUTER SCIENCE
GRAPH DATA STRUCTURE
By: Keno Benti
ID: Sgs/0645/11
JAN 10,2019
Overview
• A Graph is a non-linear data structure, which consists of vertices(or
nodes) connected by edges(or arcs) where edges may be directed
or undirected.
• Graphs are a powerful and versatile data structure that easily allow
you to represent real life relationships between different types of
data (nodes). There are two main parts of a graph:
 The vertices (nodes) where the data is stored
The edges (connections) which connect the nodes
• Vertices i and j are adjacent vertices iff (i, j) is an edge in the
graph
Graph and Its Representations
• The choice of graph representation is said to be situation
specific. It totally depends on the type of operations to be
performed and ease of use.
• Following the most commonly used representations of a graph.
1. Adjacency Matrix
2. Adjacency List
Adjacency Matrix
 is a 2D array of size V x V where V is the number of vertices in a graph. Let the
2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from
vertex i to vertex j.
 Between each pair of vertices, cost of one edge is to be stored. This
shows which vertices are adjacent to one another.
For undirected graphs half of the graph is repeated information. Hence these
matrices are said to be space inefficient.
The memory use of an adjacency matrix is O(n^2) where n = number of
vertices.
Advantages of using adjacency matrix are as follows:
 Easy to understand, implement and convenient to work with.
 Removing an edge involves time complexity of O(1).
 Queries like whether there is an edge from vertex „u‟ to
vertex „v‟ are efficient and require time complexity, O(1).
Disadvantages of using adjacency matrix are as follows:
 Consumes more space O(V^2). Even if the graph is sparse(contains
less number of edges), it consumes the same space. Adding a
vertex is O(V^2) time.
 If the number of nodes in the graph may change, matrix
representation is too inflexible (especially if we don‟t know the
maximal size of the graph).
Adjacency List
• In this representation, every vertex of graph contains list of its adjacent
vertices.
Advantages of using adjacency list are as follows:
• Addition of a vertex and connecting new vertices with the existing ones
is easier.
• Has a space complexity of O(|V|+|E|).
• It allows us to store graph in more compact form and to get the list of
adjacent vertices in O(1) time which is a big benefit for some algorithms.
Cont’d…
Disadvantages of using adjacency list are as follows:
• Queries like whether there is an edge from vertex u to vertex v are not
efficient and require time complexity, O(V).
• It does not allow us to make an efficient implementation, if dynamic
change of vertices number is required.
Types Of Graphs
1. Undirected Graph all edges are undirected or If the connection is
symmetric (in other words A is connected to B ↔ B is connected to
A), then we say the graph is undirected.
2. Directed Graph all edges are directed or If an edge only implies
one direction of connection, we say the graph is directed.
3. Weighted Graph: A weighted graph is a graph where each edge
has an associated numerical value, called weight.
Undirected Graphs
 The pair of vertices representing any edge is unordered. Thus, the
pairs (u,v) and (v,u) represent the same edge.
Example: a graph G2
• V(G2)={0,1,2,3,4,5,6}
• E( G2)={(0,1),(0,2), (1,3),(1,4),(2,5),(2,6)}
• G2 is also a tree that is a special case of graph
• An undirected graph is said to be connected if there is a path
between every pair of vertices in the graph.
Directed Graphs (Digraph)
 each edge is represented by a ordered pairs <u,v>
Example: a graph G3
• V(G3)={0,1,2}
• E(G3)={<0,1>,<1,0>,<1,2>}
• A directed path in a directed graph G is a sequence of distinct
vertices, such that there is an edge from each vertex in the
sequence to the next.
Weighted Graph
• A weighted graph is a graph where each edge has an
associated numerical value, called weight. Weighted graphs
may be either directed or undirected. The weight of the edge
is often referred to as the “cost” of the edge. Example of
weighted graph is:
Graph Traversal Algorithms
Graph traversal means visiting all the nodes of the
graph.
We want to visit every vertex and every edge exactly
once in some well-defined order.
There are two primary traversal algorithms:
• Breadth-First Search (BFS) and
• Depth-First Search (DFS).
DFS/BFS Search Algorithm
SEARCH(Node n):
1. Visit n.
2. Put n's children on a list (Stack or Queue).
3. For each child c in list:
SEARCH(c)
• If we use a Stack and recursive, we get DFS
• If we use a Queue and iterative, we get BFS
BFS-Breadth First Search
 Idea: Visit all children before first grandchild.
• Visiting order: Visit a vertex, then visit all of its neighbors,
then visit all of the neighbors of its neighbors, . . .
• BFS is that a BFS is best used at finding the shortest paths in a given
Graph
• Needs flags or someway to preventing infinite loops:
Weakness
BFS Algorithm
create a queue Q
mark v as visited and put v into Q
while Q is non-empty
remove the head u of Q
mark and enqueue all (unvisited) neighbours of u
Example : BFS
Cont’d…
Cont’d…
Cont’d…
Cont’d…
Cont’d…
Cont’d…
Cont’d…
Cont’d…
DFS-Depth First Search
• Idea: Follow a single path to its end, then backtrack
• Is an algorithm for traversing or searching a tree, tree
structure or graph.
• One starts at the root (selecting some node as the root in the
graph case) and explores as far as possible along each branch
before backtracking.
DFS ALGORITHM USING STACK
Example:DFS
Cont’d…
Cont’d…
Cont’d…
Cont’d…
Cont’d…
Cont’d…
Cont’d…
Cont’d…
Graph Traversal Algorithm Complexity
Time Complexity
• BFS Vs DFS Time complexity : O(V+E), when implemented
using the adjacency list. Where V is number of vertices in the
graph and E is number of edges in the graph.
• Space complexity (memory)
BFS :Space complexity is O(|V|) as well - since at worst case
you need to hold all vertices in the queue.
Cont’d…
• DFS: Space Complexity depends on the implementation, a
recursive implementation can have a O(h) space complexity
[worst case], where h is the maximal depth of your tree. But
Depth-first can waste time going down a “rabbit hole”, when
solution is high in tree.
• Worst Case for DFS will be the best case for BFS, and the Best
Case for DFS will be the worst case for BFS.
Conclusion
• A Graph is a non-linear data structure, which consists of vertices(or
nodes) connected by edges(or arcs) where edges may be directed or
undirected.
• Graph is represented by two important ways: Adjacency List and
Adjacency Matrix.
• Graph traversal is a process of checking or updating each vertex in a
graph.
• Graph traversal means visiting each and exactly one node.
• There are two graph traversals: Breadth first Search: Visits the neighbor
vertices before visiting the child vertices and Depth First Search:is used
for traversing a finite graph.
Recommendation
• I recommend for peoples to use hybrid (both BFS and DFS) approach. Because
Worst Case for DFS will be the best case for BFS, and the Best Case for DFS will
be the worst case for BFS in terms of space complexity.
• Building a good general-purpose graph type is surprisingly tricky and difficult.
For this reason, I suggest that you check out existing implementations
(particularly LEDA) before hacking up your own. Note that it costs only time
linear in the size of the larger data structure to convert between adjacency
matrices and adjacency lists. This conversion is unlikely to be the bottleneck in
any application, if you decide you want to use both data structures and have
the space to store them. This usually isn't necessary but might prove simplest if
you are confused about the alternatives.
References
1. Thomas H. Cormen, Charles E. Leirserson, Ronald L. Rivest, Introduction
to algorithms, Cambridge, Massachusetts London, England 2000
2. Manber, U. (1989), Introduction to Algorithms|Creative
Approach, Addison-Wesley, Reading, Massachusetts
3. Danny Sleator, “Parallel and Sequential Data Structures and
Algorithms,15-210 (fall 2013) ”, Sept. 24 , 2013
4. https://www.tutorialspoint.com/data_structures_algorithms/graph_data
_structure.htm
5. https://www.geeksforgeeks.org/graph-and-its-representations/
6. https://hackernoon.com/graphs-in-cs-and-its-traversal-algorithms-
cfee5533f74e
7. https://www.hackerearth.com/practice/algorithms/graphs/depth-first-
search/tutorial/
1 de 41

Recomendados

Graph Basic In Data structure por
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structureIkhlas Rahman
2.1K vistas12 diapositivas
Graphs In Data Structure por
Graphs In Data StructureGraphs In Data Structure
Graphs In Data StructureAnuj Modi
45.6K vistas32 diapositivas
Graphs in Data Structure por
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structurehafsa komal
2.2K vistas22 diapositivas
Application Of Graph Data Structure por
Application Of Graph Data StructureApplication Of Graph Data Structure
Application Of Graph Data StructureGaurang Dobariya
14.9K vistas20 diapositivas
Data structure - Graph por
Data structure - GraphData structure - Graph
Data structure - GraphMadhu Bala
6.3K vistas28 diapositivas
Graph traversal-BFS & DFS por
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFSRajandeep Gill
1.2K vistas35 diapositivas

Más contenido relacionado

La actualidad más candente

Adjacency list por
Adjacency listAdjacency list
Adjacency listStefi Yu
4.5K vistas15 diapositivas
Graph Data Structure por
Graph Data StructureGraph Data Structure
Graph Data StructureAfaq Mansoor Khan
300 vistas32 diapositivas
Graph in data structure por
Graph in data structureGraph in data structure
Graph in data structureAbrish06
42.8K vistas24 diapositivas
DFS and BFS por
DFS and BFSDFS and BFS
DFS and BFSsatya parsana
13.1K vistas39 diapositivas
Topological Sorting por
Topological SortingTopological Sorting
Topological SortingShahDhruv21
4.7K vistas18 diapositivas
Tree traversal techniques por
Tree traversal techniquesTree traversal techniques
Tree traversal techniquesSyed Zaid Irshad
3.1K vistas34 diapositivas

La actualidad más candente(20)

Adjacency list por Stefi Yu
Adjacency listAdjacency list
Adjacency list
Stefi Yu4.5K vistas
Graph in data structure por Abrish06
Graph in data structureGraph in data structure
Graph in data structure
Abrish0642.8K vistas
Topological Sorting por ShahDhruv21
Topological SortingTopological Sorting
Topological Sorting
ShahDhruv214.7K vistas
Presentation on Breadth First Search (BFS) por Shuvongkor Barman
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
Shuvongkor Barman17.8K vistas
Data structure computer graphs por Kumar
Data structure computer graphsData structure computer graphs
Data structure computer graphs
Kumar 4.4K vistas
Algorithms Lecture 7: Graph Algorithms por Benha University
Algorithms Lecture 7: Graph AlgorithmsAlgorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph Algorithms
Benha University3.6K vistas

Similar a Graph Data Structure

Lecture 2.3.1 Graph.pptx por
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxking779879
32 vistas23 diapositivas
Lecture 5b graphs and hashing por
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashingVictor Palmar
1.2K vistas56 diapositivas
Graphs por
GraphsGraphs
GraphsLavanyaJ28
25 vistas39 diapositivas
Graph in data structure por
Graph in data structureGraph in data structure
Graph in data structurePooja Bhojwani
196 vistas28 diapositivas
VANU no sql ppt.pptx por
VANU no sql ppt.pptxVANU no sql ppt.pptx
VANU no sql ppt.pptxMJeyavarthini
34 vistas30 diapositivas
Vanmathy no sql por
Vanmathy no sql Vanmathy no sql
Vanmathy no sql PriyadharshiniVS
30 vistas30 diapositivas

Similar a Graph Data Structure(20)

Lecture 2.3.1 Graph.pptx por king779879
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptx
king77987932 vistas
Lecture 5b graphs and hashing por Victor Palmar
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
Victor Palmar1.2K vistas
graph representation.pdf por amitbhachne
graph representation.pdfgraph representation.pdf
graph representation.pdf
amitbhachne47 vistas
Graceful labelings por radhikamathy
Graceful labelingsGraceful labelings
Graceful labelings
radhikamathy1.7K vistas
data structures and algorithms Unit 2 por infanciaj
data structures and algorithms Unit 2data structures and algorithms Unit 2
data structures and algorithms Unit 2
infanciaj65 vistas
Skiena algorithm 2007 lecture10 graph data strctures por zukun
Skiena algorithm 2007 lecture10 graph data strcturesSkiena algorithm 2007 lecture10 graph data strctures
Skiena algorithm 2007 lecture10 graph data strctures
zukun2.2K vistas
Unit VI - Graphs.ppt por HODElex
Unit VI - Graphs.pptUnit VI - Graphs.ppt
Unit VI - Graphs.ppt
HODElex11 vistas

Último

The details of description: Techniques, tips, and tangents on alternative tex... por
The details of description: Techniques, tips, and tangents on alternative tex...The details of description: Techniques, tips, and tangents on alternative tex...
The details of description: Techniques, tips, and tangents on alternative tex...BookNet Canada
121 vistas24 diapositivas
Igniting Next Level Productivity with AI-Infused Data Integration Workflows por
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Safe Software
225 vistas86 diapositivas
From chaos to control: Managing migrations and Microsoft 365 with ShareGate! por
From chaos to control: Managing migrations and Microsoft 365 with ShareGate!From chaos to control: Managing migrations and Microsoft 365 with ShareGate!
From chaos to control: Managing migrations and Microsoft 365 with ShareGate!sammart93
9 vistas39 diapositivas
Melek BEN MAHMOUD.pdf por
Melek BEN MAHMOUD.pdfMelek BEN MAHMOUD.pdf
Melek BEN MAHMOUD.pdfMelekBenMahmoud
14 vistas1 diapositiva
Five Things You SHOULD Know About Postman por
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About PostmanPostman
27 vistas43 diapositivas
DALI Basics Course 2023 por
DALI Basics Course  2023DALI Basics Course  2023
DALI Basics Course 2023Ivory Egg
14 vistas12 diapositivas

Último(20)

The details of description: Techniques, tips, and tangents on alternative tex... por BookNet Canada
The details of description: Techniques, tips, and tangents on alternative tex...The details of description: Techniques, tips, and tangents on alternative tex...
The details of description: Techniques, tips, and tangents on alternative tex...
BookNet Canada121 vistas
Igniting Next Level Productivity with AI-Infused Data Integration Workflows por Safe Software
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Safe Software225 vistas
From chaos to control: Managing migrations and Microsoft 365 with ShareGate! por sammart93
From chaos to control: Managing migrations and Microsoft 365 with ShareGate!From chaos to control: Managing migrations and Microsoft 365 with ShareGate!
From chaos to control: Managing migrations and Microsoft 365 with ShareGate!
sammart939 vistas
Five Things You SHOULD Know About Postman por Postman
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About Postman
Postman27 vistas
DALI Basics Course 2023 por Ivory Egg
DALI Basics Course  2023DALI Basics Course  2023
DALI Basics Course 2023
Ivory Egg14 vistas
Spesifikasi Lengkap ASUS Vivobook Go 14 por Dot Semarang
Spesifikasi Lengkap ASUS Vivobook Go 14Spesifikasi Lengkap ASUS Vivobook Go 14
Spesifikasi Lengkap ASUS Vivobook Go 14
Dot Semarang35 vistas
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive por Network Automation Forum
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveAutomating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Web Dev - 1 PPT.pdf por gdsczhcet
Web Dev - 1 PPT.pdfWeb Dev - 1 PPT.pdf
Web Dev - 1 PPT.pdf
gdsczhcet55 vistas
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... por Bernd Ruecker
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
Bernd Ruecker26 vistas
Special_edition_innovator_2023.pdf por WillDavies22
Special_edition_innovator_2023.pdfSpecial_edition_innovator_2023.pdf
Special_edition_innovator_2023.pdf
WillDavies2216 vistas
Report 2030 Digital Decade por Massimo Talia
Report 2030 Digital DecadeReport 2030 Digital Decade
Report 2030 Digital Decade
Massimo Talia14 vistas
Attacking IoT Devices from a Web Perspective - Linux Day por Simone Onofri
Attacking IoT Devices from a Web Perspective - Linux Day Attacking IoT Devices from a Web Perspective - Linux Day
Attacking IoT Devices from a Web Perspective - Linux Day
Simone Onofri15 vistas
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... por James Anderson
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
James Anderson33 vistas
Voice Logger - Telephony Integration Solution at Aegis por Nirmal Sharma
Voice Logger - Telephony Integration Solution at AegisVoice Logger - Telephony Integration Solution at Aegis
Voice Logger - Telephony Integration Solution at Aegis
Nirmal Sharma17 vistas
PharoJS - Zürich Smalltalk Group Meetup November 2023 por Noury Bouraqadi
PharoJS - Zürich Smalltalk Group Meetup November 2023PharoJS - Zürich Smalltalk Group Meetup November 2023
PharoJS - Zürich Smalltalk Group Meetup November 2023
Noury Bouraqadi120 vistas

Graph Data Structure

  • 1. HARAMAYA UNIVERSITY SCHOOL OF GRADUATE STUDIES DEPARTMENT OF COMPUTER SCIENCE GRAPH DATA STRUCTURE By: Keno Benti ID: Sgs/0645/11 JAN 10,2019
  • 2. Overview • A Graph is a non-linear data structure, which consists of vertices(or nodes) connected by edges(or arcs) where edges may be directed or undirected. • Graphs are a powerful and versatile data structure that easily allow you to represent real life relationships between different types of data (nodes). There are two main parts of a graph:  The vertices (nodes) where the data is stored The edges (connections) which connect the nodes • Vertices i and j are adjacent vertices iff (i, j) is an edge in the graph
  • 3. Graph and Its Representations • The choice of graph representation is said to be situation specific. It totally depends on the type of operations to be performed and ease of use. • Following the most commonly used representations of a graph. 1. Adjacency Matrix 2. Adjacency List
  • 4. Adjacency Matrix  is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j.  Between each pair of vertices, cost of one edge is to be stored. This shows which vertices are adjacent to one another. For undirected graphs half of the graph is repeated information. Hence these matrices are said to be space inefficient. The memory use of an adjacency matrix is O(n^2) where n = number of vertices.
  • 5. Advantages of using adjacency matrix are as follows:  Easy to understand, implement and convenient to work with.  Removing an edge involves time complexity of O(1).  Queries like whether there is an edge from vertex „u‟ to vertex „v‟ are efficient and require time complexity, O(1).
  • 6. Disadvantages of using adjacency matrix are as follows:  Consumes more space O(V^2). Even if the graph is sparse(contains less number of edges), it consumes the same space. Adding a vertex is O(V^2) time.  If the number of nodes in the graph may change, matrix representation is too inflexible (especially if we don‟t know the maximal size of the graph).
  • 7. Adjacency List • In this representation, every vertex of graph contains list of its adjacent vertices. Advantages of using adjacency list are as follows: • Addition of a vertex and connecting new vertices with the existing ones is easier. • Has a space complexity of O(|V|+|E|). • It allows us to store graph in more compact form and to get the list of adjacent vertices in O(1) time which is a big benefit for some algorithms.
  • 8. Cont’d… Disadvantages of using adjacency list are as follows: • Queries like whether there is an edge from vertex u to vertex v are not efficient and require time complexity, O(V). • It does not allow us to make an efficient implementation, if dynamic change of vertices number is required.
  • 9. Types Of Graphs 1. Undirected Graph all edges are undirected or If the connection is symmetric (in other words A is connected to B ↔ B is connected to A), then we say the graph is undirected. 2. Directed Graph all edges are directed or If an edge only implies one direction of connection, we say the graph is directed. 3. Weighted Graph: A weighted graph is a graph where each edge has an associated numerical value, called weight.
  • 10. Undirected Graphs  The pair of vertices representing any edge is unordered. Thus, the pairs (u,v) and (v,u) represent the same edge. Example: a graph G2 • V(G2)={0,1,2,3,4,5,6} • E( G2)={(0,1),(0,2), (1,3),(1,4),(2,5),(2,6)} • G2 is also a tree that is a special case of graph • An undirected graph is said to be connected if there is a path between every pair of vertices in the graph.
  • 11. Directed Graphs (Digraph)  each edge is represented by a ordered pairs <u,v> Example: a graph G3 • V(G3)={0,1,2} • E(G3)={<0,1>,<1,0>,<1,2>} • A directed path in a directed graph G is a sequence of distinct vertices, such that there is an edge from each vertex in the sequence to the next.
  • 12. Weighted Graph • A weighted graph is a graph where each edge has an associated numerical value, called weight. Weighted graphs may be either directed or undirected. The weight of the edge is often referred to as the “cost” of the edge. Example of weighted graph is:
  • 13. Graph Traversal Algorithms Graph traversal means visiting all the nodes of the graph. We want to visit every vertex and every edge exactly once in some well-defined order. There are two primary traversal algorithms: • Breadth-First Search (BFS) and • Depth-First Search (DFS).
  • 14. DFS/BFS Search Algorithm SEARCH(Node n): 1. Visit n. 2. Put n's children on a list (Stack or Queue). 3. For each child c in list: SEARCH(c) • If we use a Stack and recursive, we get DFS • If we use a Queue and iterative, we get BFS
  • 15. BFS-Breadth First Search  Idea: Visit all children before first grandchild. • Visiting order: Visit a vertex, then visit all of its neighbors, then visit all of the neighbors of its neighbors, . . . • BFS is that a BFS is best used at finding the shortest paths in a given Graph • Needs flags or someway to preventing infinite loops: Weakness
  • 16. BFS Algorithm create a queue Q mark v as visited and put v into Q while Q is non-empty remove the head u of Q mark and enqueue all (unvisited) neighbours of u
  • 26. DFS-Depth First Search • Idea: Follow a single path to its end, then backtrack • Is an algorithm for traversing or searching a tree, tree structure or graph. • One starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking.
  • 37. Graph Traversal Algorithm Complexity Time Complexity • BFS Vs DFS Time complexity : O(V+E), when implemented using the adjacency list. Where V is number of vertices in the graph and E is number of edges in the graph. • Space complexity (memory) BFS :Space complexity is O(|V|) as well - since at worst case you need to hold all vertices in the queue.
  • 38. Cont’d… • DFS: Space Complexity depends on the implementation, a recursive implementation can have a O(h) space complexity [worst case], where h is the maximal depth of your tree. But Depth-first can waste time going down a “rabbit hole”, when solution is high in tree. • Worst Case for DFS will be the best case for BFS, and the Best Case for DFS will be the worst case for BFS.
  • 39. Conclusion • A Graph is a non-linear data structure, which consists of vertices(or nodes) connected by edges(or arcs) where edges may be directed or undirected. • Graph is represented by two important ways: Adjacency List and Adjacency Matrix. • Graph traversal is a process of checking or updating each vertex in a graph. • Graph traversal means visiting each and exactly one node. • There are two graph traversals: Breadth first Search: Visits the neighbor vertices before visiting the child vertices and Depth First Search:is used for traversing a finite graph.
  • 40. Recommendation • I recommend for peoples to use hybrid (both BFS and DFS) approach. Because Worst Case for DFS will be the best case for BFS, and the Best Case for DFS will be the worst case for BFS in terms of space complexity. • Building a good general-purpose graph type is surprisingly tricky and difficult. For this reason, I suggest that you check out existing implementations (particularly LEDA) before hacking up your own. Note that it costs only time linear in the size of the larger data structure to convert between adjacency matrices and adjacency lists. This conversion is unlikely to be the bottleneck in any application, if you decide you want to use both data structures and have the space to store them. This usually isn't necessary but might prove simplest if you are confused about the alternatives.
  • 41. References 1. Thomas H. Cormen, Charles E. Leirserson, Ronald L. Rivest, Introduction to algorithms, Cambridge, Massachusetts London, England 2000 2. Manber, U. (1989), Introduction to Algorithms|Creative Approach, Addison-Wesley, Reading, Massachusetts 3. Danny Sleator, “Parallel and Sequential Data Structures and Algorithms,15-210 (fall 2013) ”, Sept. 24 , 2013 4. https://www.tutorialspoint.com/data_structures_algorithms/graph_data _structure.htm 5. https://www.geeksforgeeks.org/graph-and-its-representations/ 6. https://hackernoon.com/graphs-in-cs-and-its-traversal-algorithms- cfee5533f74e 7. https://www.hackerearth.com/practice/algorithms/graphs/depth-first- search/tutorial/