Lecture 2.3.1 Graph

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

UNIVERSITY INSTITUTE OF

ENGINEERING
DEPARTMENT OF COMPUTER
SCIENCE AND ENGG.
Bachelor of Engineering (Computer Science & Engineering)
DATA STRUCTURES CST-231
Prepared by: Er. Hatesh

GRAPHS DISCOVER . LEARN . EMPOWER


• https://www.thecrazyprogrammer.com/2
017/08/difference-between-tree-and-gra
GRAPHS ph.html

COURSE OUTCOMES
CO Title Level
Number

CO1 Able to develop applications using the graph data Applying


structure.

CO2 Able to compare between different types of evaluating


tree data structures and use an appropriate data  
structure for a design.

CO3 Identify the strengths and weaknesses of different data Analyzing


structures.

CO1

2
 A graph G consists of two sets
– a finite, nonempty set of vertices V(G)

GRAPHS – a finite, possible empty set of edges E(G)


– G(V,E) represents a graph
 An undirected graph is one in which the pair of vertices in a
edge is unordered, (v0, v1) = (v1,v0)
 A directed graph is one in which each edge is a directed pair of
vertices, <v0, v1> != <v1,v0>

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. ...
 Graphs are used to represent networks.

https://tekslate.com/graphs-in-data-structures

3
  of Vertices V
Set
Examples of Set of Edges E 

GRAPHS
in Facebook, each person is represented with a vertex or a node.
Each node is a structure and contains the information like user id,
user name, gender etc.
Graph 1:

V = {A, B, C, D, E, F}
E = {(A, B), (A, C), (B, C), (B, D), (D, E), (D, F), (E, F)}

Graph 2:

V = {A, B, C, D, E, F}
E = {(A, B), (A, C), (B, D), (C, E), (C, F)}

Graph 3:
https://www.tutorialride.com/data-structures/graphs-in-data-str
V = {A, B, C} ucture.htm
E = {(A, B), (A, C), (C, B)}

4
Graph
Representations
Graph is a data structure that consists of following
two components:

1. A finite set of vertices also called as nodes.


2. A finite set of ordered pair of the form (u, v)
called as edge. 
The pair is ordered because (u, v) is not same as (v,
u) in case of a directed graph(di-graph). The pair of
the form (u, v) indicates that there is an edge from http://btechsmartclass.com/data_structures/graph-representati
vertex u to vertex v. The edges may contain ons.html
weight/value/cost. /
s.
5
Adjacency Matrix •.

An adjacency matrix is a square matrix used to


represent a finite graph.
In the special case of a finite simple graph, the
adjacency matrix is a (0,1) with zeros on its diagonal.
The adjacency matrix should be distinguished from the
incidence for a graph, a different matrix representation
whose elements indicate whether vertex–edge pairs are
incident or not, and degree.

https://www.ebi.ac.uk/training/online/course/network-ana
lysis-protein-interaction-data-introduction/introduction-gra
ph-theory/graph-0

6
Adjacency List •.

Adjacency list representations of graphs take a more


vertex-centric approach.
There are many possible implementations of adjacency
lists representation of graph.
Here we will implement it in 2 ways:- one using array of
vectors and another using vector of lists.
The representation of graph is implemented using
adjacency list. It makes use of STL(Standard Template
Library of C++)
https://www.kodefork.com/articles/adjacency-list-graph-re
presentation-using-c-stl-in-competitive-programming/

7
Graph Terminology

Vertex
A individual data element of a graph is called as Vertex. Vertex is also known as node. In above
example graph, A, B, C, D & E are known as vertices.
Edge
An edge is a connecting link between two vertices. Edge is also known as Arc. An edge is represented as
(starting Vertex, ending Vertex). For example, in above graph, the link between vertices A and B is represented
as (A,B). In above example graph, there are 7 edges (i.e., (A,B), (A,C), (A,D), (B,D), (B,E), (C,D), (D,E)).

Edges are three types.


Undirected Edge - An undirected edge is a bidirectional edge. If there is a undirected edge between
vertices A and B then edge (A , B) is equal to edge (B , A).
Directed Edge - A directed edge is a unidirectional edge. If there is a directed edge between vertices A
and B then edge (A , B) is not equal to edge (B , A).
Weighted Edge - A weighted edge is an edge with cost on it.

8
Graph Terminology

Degree
Total number of edges connected to a vertex is said to be degree of that vertex.
Indegree
Total number of incoming edges connected to a vertex is said to be indegree of that vertex.
Outdegree
Total number of outgoing edges connected to a vertex is said to be outdegree of that vertex.
Parallel edges or Multiple edges
If there are two undirected edges to have the same end vertices, and for two directed edges to have the same origin and the
same destination. Such edges are called parallel edges or multiple edges.
Self-loop
An edge (undirected or directed) is a self-loop if its two endpoints coincide.
Simple Graph
A graph is said to be simple if there are no parallel and self-loop edges.
Path
A path is a sequence of alternating vertices and edges that starts at a vertex and ends at a vertex such that each edge is
incident to its predecessor and successor vertex.
http://www.csl.mtu.edu/cs2321/www/newLectures/24_Graph_Terminology.html

9
•.

Adjacent and Incident


A vertex is incident to an edge if the vertex is one of the
two vertices the edge connects. Two distinct incidences
and are adjacent 

http://www.personal.kent.edu/~rmuhamma/GraphTheory/
MyGraphTheory/defEx.htm

10
•.
Degree
 The degree of a vertex is the number of edges
incident to that vertex
 For directed graph,
– the in-degree of a vertex v is the number of
edges
that have v as the head
– the out-degree of a vertex v is the number of
edges
that have v as the tail

https://www.slideshare.net/aakashdeepsinghal/lecture-8-e

11
Connected •.
Component
 A connected component of an undirected graph
is a maximal connected subgraph.
 A tree is a graph that is connected and acyclic.
 A directed graph is strongly connected if there
is a directed path from vi to vj and also
from vj to vi.
 A strongly connected component is a maximal
subgraph that is strongly connected.
.
https://www.slideshare.net/angellaandrea/advanced-algori
thms-1-unionfind-on-disjointset-data-structures

12
Simple Path •.

 A simple path is a path in which all vertices,


except possibly the first and the last, are distinct
 A cycle is a simple path in which the first and
the last vertices are the same
 In an undirected graph G, two vertices, v0 and v1,
are connected if there is a path in G from v0 to v1
 An undirected graph is connected if, for every
pair of distinct vertices vi, vj, there is a path
from vi to vj
http://www.algolist.net/Data_structures/Graph

13
Graph Traversal •.

Graph traversal is technique used for searching a vertex


in a graph. The graph traversal is also used to decide the
order of vertices to be visit in the search process. A
graph traversal finds the egdes to be used in the search
process without creating loops that means using graph
traversal we visit all verticces of graph without getting
into looping path.

There are two graph traversal techniques and they are


as follows...
• DFS (Depth First Search)
https://
• BFS (Breadth First Search) medium.com/basecs/going-broad-in-a-graph-bfs-traversal-
959bd1a09255
14
Depth First Search •.

Depth-first search is an algorithm for traversing or


searching tree or graph data structures.
The algorithm starts at the root node and explores as far
as possible along each branch before backtracking.
DFS for a graph is similar to Depth First Transversal Of A
tree. The only catch here is, unlike trees, graphs may
contain cycles, so we may come to the same node again.

https://www.youtube.com/watch?v=iaBEKo5sM7w

15
DFS

• DFS traversal of a graph, produces a spanning tree as final result. Spanning Tree is a graph without any loops.
• We use Stack data structure with maximum size of total number of vertices in the graph to implement DFS traversal of a
graph.

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 adjacent vertex of the verex which is at top of the stack which is not visited and push it on to
the stack.
• Step 4: Repeat step 3 until there are no new vertex to be visit from the vertex on top of the stack.
• Step 5: When there is no new vertex to be 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

16
Breadth First •.
Search

Breadth-first search (BFS) is an algorithm for traversing


or searching tree or graph data structures.
It starts at the tree root (or some arbitrary node of a
graph, sometimes referred to as a 'search key'), and
explores all of the neighbor nodes at the present depth
prior to moving on to the nodes at the next depth level.
It uses the opposite strategy as depth first search, which
instead explores the highest-depth nodes first before
being forced to backtrack and expand shallower nodes.
https://artint.info/2e/html/ArtInt2e.Ch3.S5.SS1.html

17
BFS

• BFS (Breadth First Search)


• BFS traversal of a graph, produces a spanning tree as final result. Spanning Tree is a graph without any loops. We
use Queue data structure with maximum size of total number of vertices in the graph to implement BFS traversal
of a graph.

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 adjacent vertices of the verex which is at front of the Queue which is not visited and insert
them into the Queue.
• Step 4: When there is no new vertex to be visit from the vertex at front of the Queue then delete that vertex
from the Queue.
• Step 5: Repeat step 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

18
Advanced topics on Graphs

• Distances in graphs
• Disjoint Set
• Perfect Graphs
• Uniquely Partially Orderable Graphs
• Comparability Graphs

19
Assessment Pattern
• MST 1 -36 MARKS
• MST 2 -36 MARKS
• ELEMNT 1- Quiz- 12 MARKS
• ELEMENT 2 – ST – 9 MARKS
• ELEMENT 3 – assignment – 12 MARKS
• ELEMNET 4 – TUTE – 9 MARKS
• 6 MARKS FOR 90% ATTENDANCE

20
Applications
• Facebook: Each user is represented as a vertex and two people are friends when there is an edge between two vertices.
Similarly friend suggestion also uses graph theory concept.
• Google Maps: Various locations are represented as vertices and the roads are represented as edges and graph theory is
used to find shortest path between two nodes.
• Recommendations on e-commerce websites: The “Recommendations for you” section on various e-commerce websites
uses graph theory to recommend items of similar type to user’s choice.
• Graph theory is also used to study molecules in chemistry and physics.

21
REFERENCES

1. Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
2. Data structure and algorithm by Narasimha Karumanchi.
3. www.tutorialspoint.com
4. www.geeksforgeeks.com
5. Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
6. Gilberg/Forouzan,” Data Structure with C ,Cengage Learning.
7. Augenstein,Moshe J , Tanenbaum, Aaron M, “Data Structures using C and C++”, Prentice Hall of India.
8. Goodrich, Michael T., Tamassia, Roberto, and Mount, David M., “Data Structures and Algorithms in C++”, Wiley Student
Edition.
9. Aho, Alfred V., Ullman, Jeffrey D., Hopcroft ,John E. “Data Structures and Algorithms”, Addison Wesley

22
THANK YOU

For queries
Email: hatesh.cse@cumail.in

You might also like