SlideShare a Scribd company logo
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/

More Related Content

PPTX
Graph in data structure
PPT
Graph theory
PPTX
C# in depth
PPT
Introduction To C#
PPTX
Merge sort algorithm
PDF
linear search and binary search
PPTX
Design pattern-presentation
PPTX
CSharp Presentation
Graph in data structure
Graph theory
C# in depth
Introduction To C#
Merge sort algorithm
linear search and binary search
Design pattern-presentation
CSharp Presentation

What's hot (20)

PDF
UNIT I LINEAR DATA STRUCTURES – LIST
PPTX
Topological Sorting
PPTX
Graphs data structures
PPTX
Graphs in data structure
PPT
Insertion sort bubble sort selection sort
PPTX
Data structure - Graph
PPTX
Doubly Linked List
PPTX
Tree traversal techniques
PDF
Queue as data_structure
PPT
Graph coloring problem
PPTX
Queue in Data Structure
PPT
3.3 hierarchical methods
PPTX
Hierarchical Clustering | Hierarchical Clustering in R |Hierarchical Clusteri...
PPT
Data Structure and Algorithms Binary Search Tree
PPTX
Hashing in datastructure
PPTX
Tree Traversal
PPTX
Graph Data Structure
PPTX
Queue Implementation Using Array & Linked List
PPTX
Python Seaborn Data Visualization
PPTX
N queen problem
UNIT I LINEAR DATA STRUCTURES – LIST
Topological Sorting
Graphs data structures
Graphs in data structure
Insertion sort bubble sort selection sort
Data structure - Graph
Doubly Linked List
Tree traversal techniques
Queue as data_structure
Graph coloring problem
Queue in Data Structure
3.3 hierarchical methods
Hierarchical Clustering | Hierarchical Clustering in R |Hierarchical Clusteri...
Data Structure and Algorithms Binary Search Tree
Hashing in datastructure
Tree Traversal
Graph Data Structure
Queue Implementation Using Array & Linked List
Python Seaborn Data Visualization
N queen problem
Ad

Similar to Graph Data Structure (20)

PPTX
Lecture 2.3.1 Graph.pptx
PPTX
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH
PPTX
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
PPTX
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH.pptx
PPT
Lecture 5b graphs and hashing
PPT
Graphs
PPTX
Graph in data structure
PPTX
VANU no sql ppt.pptx
PPTX
Vanmathy no sql
PDF
graph representation.pdf
PDF
LEC 12-DSALGO-GRAPHS(final12).pdf
PPSX
Unit-6 Graph.ppsx ppt
PPTX
Spanningtreesppt
PPTX
Graph data structures for ppt for understanding.pptx
PDF
Graphhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.pdf
PDF
U1 L5 DAA.pdf
PDF
Unit-10 Graphs .pdf
PDF
The Graph Abstract Data Type-DATA STRUCTURE.pdf
PPT
Graceful labelings
PPTX
ppt 1.pptx
Lecture 2.3.1 Graph.pptx
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH.pptx
Lecture 5b graphs and hashing
Graphs
Graph in data structure
VANU no sql ppt.pptx
Vanmathy no sql
graph representation.pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
Unit-6 Graph.ppsx ppt
Spanningtreesppt
Graph data structures for ppt for understanding.pptx
Graphhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.pdf
U1 L5 DAA.pdf
Unit-10 Graphs .pdf
The Graph Abstract Data Type-DATA STRUCTURE.pdf
Graceful labelings
ppt 1.pptx
Ad

Recently uploaded (20)

PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Cloud computing and distributed systems.
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Empathic Computing: Creating Shared Understanding
Reach Out and Touch Someone: Haptics and Empathic Computing
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Electronic commerce courselecture one. Pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Dropbox Q2 2025 Financial Results & Investor Presentation
Cloud computing and distributed systems.
Building Integrated photovoltaic BIPV_UPV.pdf
Big Data Technologies - Introduction.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Spectral efficient network and resource selection model in 5G networks
Understanding_Digital_Forensics_Presentation.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
The AUB Centre for AI in Media Proposal.docx
“AI and Expert System Decision Support & Business Intelligence Systems”
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Network Security Unit 5.pdf for BCA BBA.
Empathic Computing: Creating Shared Understanding

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/