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

Graph Class Notes

Uploaded by

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

Graph Class Notes

Uploaded by

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

Graphs &

Applications
Graph
A graph G consists of two sets:
 a set V of vertices, or nodes, and
 a set E of edges that connect the vertices.
What cases to use graph
 Commonly Used in Maps
Graph vs Tree
1. Graph need not be completely connected
 Whereas tree will be completely connected
2. Graph may have cycles
 Where as Tree will not have cycles
Sub Graph
A subgraph consists of a subset of a
graph’s vertices and a subset of its edges.
Adjacent vertices
 Twovertices of a Graph are adjacent if
they are joined by an edge.
Path
A path between two vertices is a
sequence of edges that begins at one
vertex and ends at another vertex
Cycle
A cycle is a path that begins and ends at
the same vertex;
 a simple cycle is a cycle that does not
pass through other vertices more than
once.
Connected Graph
A graph is connected if each pair of
distinct vertices has a path between
them.
 That is, in a connected graph you can get
from any vertex to any other vertex by
following a path.
Connected Graph eg:-
Disconnected Graph
Complete Graph
 Isa graph is which there is an edge
between each pair of vertces.
 A complete graph has an edge between
each pair of distinct vertices
Below are not graphs
a graph cannot have duplicate edges
between vertices. If duplicate edges are
there we will call it a multigraph

 Multi graph is not a graph


Below are not graphs
 A graph’s edges cannot begin and end at the
same vertex.

 Edges that start and end at the same vertex are


called self edge, or loop
weighted graph
A graph in which label/weights assigned
to edges
Directed Graph
 Edges in a directed graph have direction so
that we can traverse only in that direction

 When there is no direction for edges; it is an


undirected Graph.
 In an undirected graph we can traverse in
both direction.
Graphs as ADTs
Operations of the ADT Graph
1. Create an empty graph.
2. Determine whether a graph is empty.
3. Determine the number of vertices in a graph.
4. Determine the number of edges in a graph.
5. Determine whether an edge exists between two given vertices.
For weighted graphs, return weight value.
6. Insert a vertex in a graph whose vertices have distinct search
keys that differ from the new vertex’s search key.
7. Insert an edge between two given vertices in a graph.
8. Delete a particular vertex from a graph and any edges between
the vertex and other vertices.
9. Delete the edge between two given vertices in a graph.
10. Retrieve from a graph the vertex that contains a given search ke
2 ways of Implementing Graphs
1. 2D Array Represenation - adjacency matrix
2. References - adjacency list
adjacency matrix
 In a graph with n vertices; Adjacency matrix is an
n by n matrix.
 Any array element; matrix[i][j] will have value 1 when
there is an edge from vertex i to vertex j
 In a weighted graph matrix[i][j] will have the weight
of the edge from node I to node j.
 When noedge is there between node 1 & node j;
then Matrix[i][j] will be infinity.
 Diagonal elements of the array; matrix[i][i] will have
value 0.
Eg:- weighted directed
graph
Eg:- weighted undirected graph
adjacency list
 An adjacency list for a graph with n vertices
numbered 0, 1, etc to n – 1 consists of n
linked lists.
 The ith linked list has a node for vertex j if and
only if the graph contains an edge from
vertex i to vertex j.
 This node can contain the vertex j’s value, if
any.
Eg:
2 common operations
. Determine whether there is an edge
from vertex i to vertex j 2.
 Find all vertices adjacent to a given
vertex i
Graph Traversals
 Ina connected Graph; traversal starts at
any node in the graph and visits all the
other nodes.
 2 Traversal methods
 Breadth-First Search
 Depth-First Search
Breadth-First Search
 Choose any Vertex from the graph and visit
(process) all its connected vertices.
 Choose any one Vertex from the connected
vertices visit all its connected Vertices.
 Select any one Vertex from the visited & repeat
the same step
 A Vertex once visited will be marked & will not
be visited again
Breadth-First Search
 Breadth First Traversal (BFS) uses a Queue
for implementation
Depth-First Search
 Choose any vertex from the graph and
visit (process) it.
 Visit one of its connected vertices & process
it
 Repeat the step till no vertex left
 A Vertex once visited will be marked & will
not be visited again
Depth-First Search
 Depth First Search uses a stack for
implementataion

You might also like