0% found this document useful (0 votes)
12 views52 pages

7001 Ds Graph

Uploaded by

srk712906
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views52 pages

7001 Ds Graph

Uploaded by

srk712906
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Data Structure: GRAPH

Dr. Bibhudatta Sahoo


Communication & Computing Group
Department of CSE, NIT Rourkela
Email: [email protected], 9937324437, 2462358
Educational Use Disclaimer
 This teaching material is provided for educational purposes only and includes
information and data compiled from various third-party sources. The
inclusion of third-party content is intended solely for the enhancement of
instruction and learning within the classroom setting at NIT Rourkela.
 Attributions: All content, copyrights, and trademarks are property of their
respective owners. The materials are used in this course under the provisions
of fair use and with no intent of infringement. Wherever feasible, specific
attributions and references to the original sources are provided. We
encourage students and educators to always respect copyright and
intellectual property rights and to consult original sources where possible.
 Limitations of Use: These materials are not to be used for commercial
purposes and are intended to serve as supplementary educational content.
Any distribution or reproduction for commercial purposes is strictly prohibited
without express permission from the copyright holders.
 Disclaimer of Liability: NIT Rourkela does not claim ownership of the
underlying copyright materials. The institution accepts no liability for the
accuracy, legality, or content of the external site or for that of subsequent
links. Contact the external site for answers to questions regarding its content.
 Contact Information: For any inquiries regarding the use of these materials
or to request additional permissions, please contact [email protected].

2 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024


urkela
Non-linear data structure: GRAPH

3 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024


urkela
Suggested Reading

4 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024


urkela
Non-linear Data Structure Graph
 In Computer Science, a graph is a non-linear data
structure, that consists of nodes and edges.
 Graphs are powerful modeling tools that are used
to solve real-world problems. We can model a
particular problem in a graph and then apply
some graph algorithms to find the solution.
 A very common use of graphs is networks’
representation such as computer or telephone
networks, city roads and points of interest, etc.
 Even in social media like Facebook and Linked In,
we can use graphs to represent the friends or
connections between the uses. In this case, each
user is represented as a node and is connected
with the nodes that are friends.
5 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024
urkela
Data Structures: Graph Theory
Prerequisites:
 Introduction to Programming (preferably in Java,
C++, or Python)
 Basic knowledge of Discrete Mathematics
Course Objectives:
 Understand and apply various graph
representations and traversal techniques.
 Solve problems using graph algorithms, including
shortest path, minimum spanning tree, and
network flow.
 Analyze the computational complexity of graph
algorithms.
 Develop algorithms for real-world applications
6 using
Data graphGraph
Structures:
ourkela
concepts.
, Prof. Bibhudatta Sahoo@NIT R 12/11/2024
7 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024
ourkela
Graph Data Structure
A Graph is a non-linear data structure
consisting of vertices and edges. The
vertices are sometimes also referred to as
nodes and the edges are lines or arcs that
connect any two nodes in the graph. More
formally a Graph is composed of a set of
vertices( V ) and a set of edges( E ). The
graph is denoted by G(V, E).

8 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024


ourkela
Components of a Graph
 Vertex: A vertex is regarded as the fundamental
unit of a graph. It is sometimes called a node. A
node or vertex can be labeled or it can be
unlabeled.
 Edge: The main purpose of an edge is to connect
two vertices. Two nodes can be connected with an
edge in any possible way. Just like a vertex, an
edge can be labeled or unlabeled. An edge could
carry some integer value as well known as
weight.

9 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024


urkela
Undirected & Directed Graph
 G(V, E) : V ={A, B, C, D, E} E = {AB, BC, AC, BD,
ED, EC, CD}
OR

 G(V, E) : V ={A, B, C, D, E} E = {BA, BC, CA, DB,


ED, CC, DC}

 G(V, E) : V ={A, B, C, D, E} E = {AB, BC, BD,


ED, CE, DC}

10 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024


ourkela
Graph Data Structure
A Graph is a non-linear data structure
consisting of vertices and edges. The
vertices are sometimes also referred to as
nodes and the edges are lines or arcs that
connect any two nodes in the graph. More
formally a Graph is composed of a set of
vertices( V ) and a set of edges( E ). The
graph is denoted by G(V, E).

11 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024


ourkela
Types of Graphs:
 Directed Graph: The edges have a direction,
pointing from one vertex to another, like a one-
way street.
 Undirected Graph: The edges have no direction,
indicating a two-way connection, like an
undirected road.
 Weighted Graph: Each edge has a weight or
cost associated with it, often used to represent
distances or costs.
 Unweighted Graph: The edges have no weight,
representing only connections.
 Cyclic Graph: Contains at least one cycle (a path
that starts and ends at the same vertex).
 Acyclic Graph: Has no cycles.
12 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024
urkela
Graph Terminology
 Path: A path can be defined as the sequence of
nodes that are followed in order to reach some
terminal node V from the initial node U.
 Closed Path: A path will be called as closed path
if the initial node is same as terminal node. A
path will be closed path if V0=VN.
 Simple Path: If all the nodes of the graph are
distinct with an exception V0=VN, then such path
P is called as closed simple path.

13 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024


urkela
Graph Terminology
 Cycle: A cycle can be defined as the path which has no
repeated edges or vertices except the first and last vertices.
 Connected Graph: A connected graph is the one in which
some path exists between every two vertices (u, v) in V.
There are no isolated nodes in connected graph.

 Complete Graph: A complete graph is the one in which


every node is connected with all other nodes. A complete
graph contain n(n-1)/2 edges where n is the number of
nodes in the graph.
14 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024
urkela
Graph Terminology
 Weighted Graph: In a weighted graph, each
edge is assigned with some data such as length
or weight. The weight of an edge e can be given
as w(e) which must be a positive (+) value
indicating the cost of traversing the edge.
 Digraph: A digraph is a directed graph in which
each edge of the graph is associated with some
direction and the traversing can be done only in
the specified direction.

15 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024


urkela
Graph Terminology
 Loop: An edge that is associated with the similar
end points can be called as Loop.
 Adjacent Nodes: If two nodes u and v are
connected via an edge e, then the nodes u and v
are called as neighbours or adjacent nodes.
 Degree of the Node: A degree of a node is the
number of edges that are connected with that
node. A node with degree 0 is called as isolated
node.

16 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024


urkela
Type of Graph

17 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024


ourkela
Undirected Graph representation

18 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024


ourkela
Directed Graph representation

19 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024


ourkela
Weighted Directed Graph representation

20 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024


ourkela
The adjacency matrix representation of a graph

21 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024


ourkela
The adjacency matrix representation of a graph

22 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024


ourkela
1. Adjacency Matrix

 Pros: Simple and straightforward; efficient for


dense graphs and quick for operations like
checking if there is an edge between two
vertices.
 Cons: Uses O(|V|2) memory even if the graph has
very few edges, making it inefficient for sparse
graphs.
23 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024
ourkela
1. Adjacency Matrix
 Applications: Adjacency matrices are ideal when
the graph is dense, or when operations like
checking whether there is an edge between two
vertices need to be very efficient (constant time
operation).
 Adjacency matrices are also used in algorithms
that require frequent matrix operations, such as
computing transitive closures or paths of certain
lengths.

24 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024


ourkela
Example of an Adjacency Matrix

25 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024


ourkela
Adjacency Matrix of a undirected Graph

26 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024


urkela
2. Adjacency List
An adjacency list for each vertex stores a list (or a linked
list) of its neighbor vertices. This structure can also be
enhanced to store edge weights by including weight values
alongside neighbors in the list.

 Pros: Useful for edge-centric operations or algorithms. It


makes it easy to see which vertices are connected by an
edge.
 Cons: Consumes more space than an adjacency list for
sparse graphs and is less intuitive than an adjacency
matrix.
 Applications: Adjacency lists are commonly used
because they efficiently represent sparse graphs. They
are preferred in scenarios where space complexity might
be a concern and when it’s common to iterate over all
edges of a particular vertex.
27 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024
ourkela
Undirected Graph representation with
Adjacency list

28 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024


ourkela
Directed Graph representation with Adjacency list

29 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024


ourkela
Weighted directed graph representation with
adjacency list

30 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024


ourkela
3. Incidence Matrix

 Applications: Incidence matrices are useful for


checking vertex-edge relationships quickly and
are used in some algorithms dealing with flows or
connectivity issues.

31 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024


ourkela
Incidence Matrix

32 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024


ourkela
Incidence Matrix
• The graph is represented using a matrix of size
total number of vertices by a total number of
edges
• The matrix is filled with 0 or 1 or -1. where, 0
represents that the row edge is not connected to
column vertex, 1 represents that the row edge is
connected as the outgoing edge to column vertex
and -1 represents that the row edge is connected
as the incoming edge to column vertex.

33 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024


ourkela
4. Edge List
 An edge list is a simple list of all the edges in the
graph, where each edge is represented as a pair
(or tuple) of vertices.
 Pros: Very simple structure; efficient in terms of
space, especially if the graph has very few edges.
 Cons: Operations like checking whether an edge
exists between two vertices or finding the
neighbors of a vertex can be slower unless the list
is kept sorted.
 Applications: Edge lists are the simplest form of
graph representation, used when the primary
operations involve processing all edges
regardless of their endpoints. They are
particularly efficient when the graph is very
Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024
34
sparse or when the graph needs to be serialized
ourkela
5. Object-Oriented Representation
 In object-oriented programming languages, graphs can
also be represented using classes for vertices and
edges. Each vertex can have a list of Edge objects, each
of which references other vertices.
 Pros: Highly flexible and intuitive, especially for
complex graphs where vertices and edges have many
attributes.
 Cons: Memory usage is higher, and operations can be
slower than in simpler representations.
 Applications: This is particularly useful in software
design where the graph needs to be manipulated
dynamically with complex interactions and updates. It
allows for more flexibility and encapsulation,
accommodating a variety of graph operations with
additional methods and properties.
35 Data Structures: Graph , Prof. Bibhudatta Sahoo@NIT R 12/11/2024
ourkela
Linked representation of a Graph

36 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024


urkela
Abstract Data Type (ADT) for a graph
 An Abstract Data Type (ADT) for a graph
specifies the fundamental operations that can be
performed on a graph without detailing the
internal implementation.
 A graph ADT defines a set of operations to
create and manipulate a graph's vertices and
edges.
Operations

37 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024


urkela
Operations with GRAPH ADT

38 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024


urkela
Operations with GRAPH ADT

39 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024


urkela
Operations with GRAPH ADT

40 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024


urkela
Operations with GRAPH ADT
Optional Operations
1. isEmpty(): Checks if the graph has any
vertices.
2. size(): Returns the number of vertices (and
possibly edges) in the graph.
3. degree(vertex): Returns the degree (number
of connections) of a specified vertex in an
undirected graph or the in/out degree in a
directed graph.

41 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024


urkela
42 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024
urkela
Thanks for Your Attention!

44 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024


urkela
References
 https://
python.plainenglish.io/graph-data-structure-theor
y-and-python-implementation-ee8c9795eae7
 http://
www.btechsmartclass.com/data_structures/graph-
traversal-bfs.html
 https://ics.uci.edu/~thornton/ics46/Notes/Shortest
Paths
/

45 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024


urkela
Suggested Exercise
1. What is a graph in data structures? Explain its basic components.
2. What is the difference between a directed and an undirected
graph?
3. How can graphs be represented using an adjacency list and an
adjacency matrix?
4. What is the difference between a sparse graph and a dense graph?
5. What is a complete graph? How many edges are there in a
complete graph with `n` vertices?
6. Explain the difference between a weighted and an unweighted
graph.
7. What is a subgraph? How does it differ from the original graph?
8. What is an acyclic graph? Give an example of a real-world
application.
9. What is the degree of a vertex in an undirected graph? How does
the degree of a vertex in a directed graph differ?
10. What is a tree in graph theory? How is it different from a general
graph?

46 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024


urkela
11. What is the shortest path in a graph? Name two algorithms that can
be used to find it.
12. What is graph traversal? Compare Depth-First Search (DFS) and
Breadth-First Search (BFS).
13. What is a connected component in a graph? How can you
determine the number of connected components in an undirected
graph?
14. Define a path in a graph. What is a simple path?
15. What is the difference between an Eulerian Path and a Hamiltonian
Path?
16. What is an adjacency matrix? How does it represent a graph?
17. What is an adjacency list? Why is it more space-efficient than an
adjacency matrix?
18. Write a function to check if a given undirected graph is connected.
19. How do you determine all vertices in a graph that have a degree
greater than 2?
20. Design an algorithm to count the number of edges in a graph
represented by an adjacency matrix.
47 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024
urkela
21. How would you find all vertices with no incoming edges in a
directed graph?
22. What is the difference between a cycle and a simple cycle in a
graph?
23. What is a bipartite graph? How can you check if a graph is bipartite?
24. What is a multigraph, and how does it differ from a simple graph?
25. Explain the concept of a weighted graph and its application in real-
world problems.
26. What is a cut vertex (or articulation point) in a graph? How would
you identify one?
27. What is a bridge (or cut-edge) in a graph? How can it affect the
structure of the graph?
28. How would you perform a topological sort of a directed acyclic
graph (DAG)?
29. What is a self-loop in a graph? How is it represented in an adjacency
matrix?
30. What is a directed acyclic graph (DAG), and where is it commonly
used?
48 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024
urkela
Suggested Exercise
31. What is a graph? Explain the difference between a directed and an
undirected graph.
32. Explain how graphs can be represented using an adjacency list and
adjacency matrix. What are the trade-offs between these two
representations?
33. Define the terms vertex, edge, degree, and path in the context of a graph.
34. Given a graph, write an algorithm to perform a Depth-First Search (DFS).
What is the time complexity of this algorithm?
35. Write a function to detect a cycle in a directed graph using Depth-First
Search.
36. What is a bipartite graph? Write an algorithm to check whether a given
graph is bipartite.
37. Given a directed acyclic graph (DAG), explain how to perform a topological
sort. What are the practical applications of topological sorting?
38. Given a graph representing friendships on a social network, where
vertices represent users and edges represent friendships, write a function
to find all users who are reachable from a given user.
39. Given a graph representing courses and prerequisites, write an algorithm
to check if you can complete all the courses.
40. Suppose you are given an undirected graph. Write an algorithm to
determine whether the graph is connected.
49 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024
urkela
Data Structures References
51 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024
urkela
52 Graph Data Structures , Prof. Bibhudatta Sahoo@NIT Ro 12/11/2024
urkela

You might also like