Unit 5 Session 1 - Graph
Unit 5 Session 1 - Graph
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
18CSC201J
DATA STRUCTURES AND ALGORITHMS
Unit- V
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
• GRAPH TERMINOLOGY
• GRAPH TRAVERSAL
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
Definition
• A graph G is defined as an ordered set (V, E), where V(G)
represent the set of vertices and E(G) represents the edges
that connect the vertices.
• The figure given shows a graph with V(G) = { A, B, C, D
and E} and E(G) = { (A, B), (B, C), (A, D), (B, D), (D, E),
(C, E) }. Note that there are 5 vertices or nodes and 6
edges in the graph.
A B C
Fig:a
Definition D E
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
Fig:a
D E
A B C
Fig:bDefinition
D E
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
Graph Terminology:
Adjacent Nodes or Neighbors:
For every edge, e = (u, v) that connects nodes u and v;
the nodes u and v are the end-points and are said to
be the adjacent nodes or neighbors.
Degree of a node:
Degree of a node u, deg(u), is the total number of
edges containing the node u. If deg(u) = 0, it means
that u does not belong to any edge and such a node is
known as an isolated node.
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
3 4
e1
A B C
e4 A B C
A B
e2
2
e3
7 1
C e6 D E F
e5 e7 D B
3
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
Directed Graph:
• A directed graph G, also known as a digraph, is a graph in which
every edge has a direction assigned to it. An edge of a directed
graph is given as an ordered pair (u, v) of nodes in G. For an edge
(u, v)-
Definition
For a directed graph G = (V,E), where V is the set of vertices and
E is the set of edges, the transitive closure of G is a graph G* =
(V,E*). In G*, for every vertex pair v, w in V there is an edge (v,
w) in E* if and only if there is a valid path from v to w in G.
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
Graph Representation:
• There are three common ways of storing graphs in the computer’s
memory.
• They are:
• Sequential representation by using an adjacency matrix.
• Linked representation by using an adjacency list that stores the
neighbors of a node using a linked list.
• Adjacency multi-list which is an extension of linked
representation.
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
• An adjacency matrix is used to represent which nodes are adjacent to one another.
By definition, two nodes are said to be adjacent if there is an edge connecting
them.
• In this representation, graph can be represented using a matrix of size V*V.
• No matter how few edges the graph has, the matrix has O(n^2) in memory.
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
• Completeness
• Breadth-first search is said to be a complete algorithm because
if there is a solution, breadth-first search will find it regardless
of the kind of graph.
• But in case of an infinite graph where there is no possible
solution, it will diverge.
• Optimality
• Breadth-first search is optimal for a graph that has edges of
equal length, since it always returns the result with the fewest
edges between the start node and the goal node.
• But generally, in real-world applications, we have weighted
graphs that have costs associated with each edge, so the goal
next to the start does not have to be the cheapest goal available.
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.