0% found this document useful (0 votes)
7 views

Graphs

Uploaded by

aamirshahad3321
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Graphs

Uploaded by

aamirshahad3321
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Graphs

Data Structure
Introduction
• A graph data structure is a collection of nodes that have
data and are connected to other nodes.
• For example. On facebook, everything is a node. That
includes User, Photo, Album, Event, Group, Page,
Comment, Story, Video. Anything that has data is a
node.
• Every relationship is an edge from one node to another.
Whether you post a photo, join a group, like a page,
etc., a new edge is created for that relationship.
Introduction
• All of facebook is then a collection of these nodes and
edges. This is because facebook uses a graph data
structure to store its data.

• A graph is a collection of two sets V and E where V is a


finite non-empty set of vertices and E is a finite non-
empty set of edges.
• Vertices are nothing but the nodes in the graph.
• Two adjacent vertices are joined by edges.
• Any graph is denoted as G = {V, E}.
Visual Represention
Graph Terminology
• Adjacency: A vertex is said to be adjacent to another vertex if there is
an edge connecting them. Vertices 0 and 2 are not adjacent because
there is no edge between them.
• Path: A sequence of edges that allows you to go from vertex A to
vertex B is called a path. 0-1, 1-2 and 0-4, 4-3,3-2 are paths from
vertex 0 to vertex 2.
• Directed Graph: Graphs can be directed or undirected, meaning that
edges have a specific direction or they do not.
• A directed graph is also known as a digraph.
Differences b/w graph and tree
Graphs Tree
It is a collection of vertices/nodes
It is a collection of nodes and
Vertices and edges.
edges.
Each node can have any number of If there is n nodes then there
Edges
edges. would be n-1 number of edges
Types of Edges They can be directed or undirected They are always directed
There is no unique node called root There is a unique node called
Root node
in graph. root(parent) node in trees.
Traversal For graph traversal, we use We traverse a tree using
Breadth-First Search (BFS), and in-order, pre-order, or post-order
Depth-First Search (DFS). traversal methods.
For finding shortest path in For game trees, decision trees, the
Applications
networking graph is used. tree is used.
Representations of Graphs:
• The following two are the most commonly used representations of a
graph.
• Adjacency Matrix
• Adjacency List
Adjacency Matrix
• In this representation, the graph is represented using a matrix of size
total number of vertices by a total number of vertices. That means a
graph with 4 vertices is represented using a matrix of size 4X4. In this
matrix, both rows and columns represent vertices. This matrix is filled
with either 1 or 0. Here, 1 represents that there is an edge from row
vertex to column vertex and 0 represents that there is no edge from
row vertex to column vertex.
Adjacency Matrix for undirected
graph
Adjacency Matrix for directed graph
Adjacency List
• In this representation, every vertex of a graph contains list of its
adjacent vertices.
Graph Traversal
• There are two graph traversal techniques and they are as follows...
• DFS (Depth First Search)
• BFS (Breadth First Search)
DFS (Depth First Search)
DFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a
graph without loops. We use Stack data structure with maximum size of total
number of vertices in the graph to implement DFS traversal.

We use the following steps to implement DFS traversal...


• Step 1 - Define a Stack of size total number of vertices in the graph.
• Step 2 - Select any vertex as starting point for traversal. Visit that vertex and push
it on to the Stack.
• Step 3 - Visit any one of the non-visited adjacent vertices of a vertex which is at
the top of stack and push it on to the stack.
DFS (Depth First Search)
• Step 4 - Repeat step 3 until there is no new vertex to be visited from
the vertex which is at the top of the stack.
• Step 5 - When there is no new vertex to visit then use back
tracking and pop one vertex from the stack.
• Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
• Step 7 - When stack becomes Empty, then produce final spanning tree
by removing unused edges from the graph
BFS (Breadth First Search)
BFS traversal of a graph produces a spanning tree as final result. Spanning
Tree is a graph without loops. We use Queue data structure with
maximum size of total number of vertices in the graph to implement BFS
traversal.

We use the following steps to implement BFS traversal...


• Step 1 - Define a Queue of size total number of vertices in the graph.
• Step 2 - Select any vertex as starting point for traversal. Visit that vertex
and insert it into the Queue.
• Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at
front of the Queue and insert them into the Queue.
BFS (Breadth First Search)
• Step 4 - When there is no new vertex to be visited from the vertex
which is at front of the Queue then delete that vertex.
• Step 5 - Repeat steps 3 and 4 until queue becomes empty.
• Step 6 - When queue becomes empty, then produce final spanning
tree by removing unused edges from the graph

You might also like