m2 U2 Graphs
m2 U2 Graphs
Unit-2: Graphs
Syllabus
• Graphs: Basic Terminology, Types of Graphs, Graphs
representations – adjacency matrix, adjacency list; Traversals
- breath first search and depth first search; Applications of
graphs.
• Hashing: Introduction, Different hash functions, collision:
avoidance and handling methods.
Graphs-Introduction
• A graph is an abstract data structure that is used to implement the
mathematical concept of graphs.
• It is basically a collection of vertices (also called nodes) and edges that
connect these vertices.
• A graph is often viewed as a generalization of the tree structure,
where instead of having a purely parent-to-child relationship between
tree nodes, any kind of complex relationship can exist
Why are Graphs Useful?
• Graphs are widely used to model any situation where entities or
things are related to each other in pairs.
• For example, the following information can be represented by graphs:
• Family trees in which the member nodes have an edge from parent to
each of their children.
• Transportation networks in which nodes are airports, intersections,
ports, etc.
• The edges can be airline flights, one-way roads, shipping routes, etc.
Definition
• A graph G is defined as an ordered set (V, E), where V(G) represents
the set of vertices and E(G) represents the edges that connect these
vertices.
• Fig shows a graph with
and
Note that there are five vertices or nodes and six edges
in the graph.
Types of graphs
• A graph can be directed or undirected.
• In an undirected graph, edges do not have any direction associated with
them.
• That is, if an edge is drawn between nodes A and B, then the nodes can be
traversed from A to B as well as from B to A.
• In a directed graph, edges form an ordered
pair.
• If there is an edge from A to B, then there is a
path from A to B but not from B to A.
• The edge (A, B) is said to initiate from node A
(also known as initial node) and terminate at
node B (terminal node).
Graph Terminology
• Adjacent nodes or neighbours 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 neighbours.
• 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.
• Path A path P written as P = {, , , ..., ), of length n from a node u to v is
defined as a sequence of (n+1) nodes.
• Here, u = , v = and is adjacent to for i = 1, 2, 3, ..., n.
Graph Terminology
• Regular graph It is a graph where each vertex has the same number
of neighbours. That is, every node has the same degree.
• A regular graph with vertices of degree k is called a k–regular graph or
a regular graph of degree k.
Graph Terminology
• Closed path A path P is known as a closed path if the edge has the
same end-points. That is, if = .
• Simple path A path P is known as a simple path if all the nodes in the
path are distinct with an exception that may be equal to .
• If = , then the path is called a closed simple path.
• Cycle A path in which the first and the last vertices are same.
• A simple cycle has no repeated edges or vertices (except the first and
last vertices).
Graph Terminology
• Connected graph A graph is said to be connected if for any two
vertices (u, v) in V there is a path from u to v.
• That is, there are no isolated nodes in a connected graph.
• A connected graph that does not have any cycle is called a tree.
Therefore, a tree is treated as a special graph
Graph Terminology
• Complete graph A graph G is said to be complete
if all its nodes are fully connected.
• That is, there is a path from one node to every
other node in the graph.
• A complete graph has n(n–1)/2 edges, where n is
the number of nodes in G.
• Clique In an undirected graph G = (V, E), clique is a
subset of the vertex set , such that for every two
vertices in C, there is an edge that connects two
vertices.
Graph Terminology
• Labelled graph or weighted graph A graph is said to be labelled if
every edge in the graph is assigned some data.
• In a weighted graph, the edges of the graph are assigned some weight
or length.
• The weight of an edge denoted by w(e) is a positive value which
indicates the cost of traversing the edge.
Graph Terminology
• Multiple edges Distinct edges which connect the
same end-points are called multiple edges.
• That is, e = (u, v) and e' = (u, v) are known as
multiple edges of G.
• Loop An edge that has identical end-points is
called a loop.
• That is, e = (u, u).
• Multi-graph A graph with multiple edges and/or
loops is called a multi-graph.
• Size of a graph The size of a graph is the total
number of edges in it.
Directed graphs
• 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):
• The edge begins at u and terminates at v.
• u is known as the origin or initial point of e. Correspondingly, v is known
as the destination or terminal point of e.
• u is the predecessor of v. Correspondingly, v is the successor of u.
• Nodes u and v are adjacent to each other.
Digraph Terminology
• Out-degree of a node The out-degree of a node u, written as outdeg(u), is
the number of edges that originate at u.
• In-degree of a node The in-degree of a node u, written as indeg(u), is the
number of edges that terminate at u.
• Degree of a node The degree of a node, written as deg(u), is equal to the
sum of the in-degree and out-degree of that node. Therefore, deg(u) =
indeg(u) + outdeg(u).
• Source A node u is known as a source if it has a positive out-degree but a
zero in-degree.
• Sink A node u is known as a sink if it has a positive in-degree but a zero out-
degree.
Digraph Terminology
• Reachability A node v is said to be reachable from node u, if and only
if there exists a (directed) path from node u to node v.
• Strongly connected directed graph A digraph is said to be strongly
connected if and only if there exists a path between every pair of
nodes in G. That is, if there is a path from node u to v, then there
must be a path from node v to u.
• Unilaterally connected graph A digraph is said to be unilaterally
connected if there exists a path between any pair of nodes u, v in G
such that there is a path from u to v or a path from v to u, but not
both.
Digraph Terminology
• Weakly connected digraph A directed graph is said to be weakly
connected if it is connected by ignoring the direction of edges.
That is, in such a graph, it is possible to reach any node from any
other node by traversing edges in any direction (may not be in
the direction they point). The nodes in a weakly connected
directed graph must have either out-degree or in-degree of at
least 1.
• Simple directed graph A directed graph G is said to be a simple
directed graph if and only if it has no parallel edges. However, a
simple directed graph may contain cycles with the exception that
it cannot have more than one loop at a given node.
• Parallel/Multiple edges Distinct edges which connect the same
end-points are called multiple edges. That is, e = (u, v) and e' =
(u, v) are known as multiple edges of G. In Fig. 13.5(a), e3 and e5
are multiple edges connecting nodes C and D.
REPRESENTATION OF GRAPHS
• 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
neighbours of a node using a linked list.
• Adjacency multi-list which is an extension of linked representation.
Adjacency Matrix
representation
• An adjacency matrix represents which nodes are adjacent to one another.
• By definition, two nodes are said to be adjacent if there is an edge connecting them.
• In a directed graph G, if node v is adjacent to node u, then there is an edge from u to v.
• For any graph G having n nodes, the adjacency matrix will have the dimension of n X n.
• In an adjacency matrix, the rows and columns are labelled by graph vertices. An entry
aij in the adjacency matrix will contain 1, if vertices vi and vj are adjacent to each other.
• However, if the nodes are not adjacent, aij will be set to zero.
Adjacency Matrix
representation
• Since an adjacency matrix contains only 0s and 1s, it is called a bit
matrix or a Boolean matrix.
• The entries in the matrix depend on the ordering of the nodes in G.
Therefore, a change in the order of the nodes will result in a different
adjacency matrix.
Adjacency List Representation
• An adjacency list is another way
in which graphs can be
represented in the computer’s
memory.
• This structure consists of a list of
all nodes in G.
• Furthermore, every node is in
turn linked to its own list that
contains the names of all other
nodes that are adjacent to it.
Adjacency Multi-list
Representation
• modified version of adjacency lists
• Adjacency multi-list is an edge-based rather than a vertex-based
representation of graphs.
• A multi-list representation consists of two parts—a directory of
nodes’ information and a set of linked lists storing information about
edges.
GRAPH TRAVERSAL
ALGORITHMS
• The method of examining the nodes and edges of the graph is known
as graph traversal.
• There are two standard methods of graph traversal:
1. Breadth-first search(BFS)
2. Depth-first search(DFS)
• While BFS uses a queue as an auxiliary data structure to store nodes
for further processing, the DFS scheme uses a stack.
Breadth-First Search Algorithm
• Also called Level Order Traversal
• BFS is a graph search algorithm that begins at the root node and explores all the
neighbouring nodes.
• Then for each of those nearest nodes, their unvisited neighbour nodes are explored,
and so on, until it finds the goal.
Visited A
Queue B C D
Example-2
• Find the BFS traversal for the given node. Consider A
as Source node
Next, visit the first node from the Queue B and write its adjacent nodes E in queue
Visited A B
Queue C D E
Example-2
• Find the BFS traversal for the given node. Consider A
as Source node
Next, visit the first node from the Queue C and insert its unvisited adjacent nodes G in the queue
Visited A B C
Queue D E G
Example-2
• Find the BFS traversal for the given node. Consider A
as Source node
Next, visit the first node from the Queue D and insert its unvisited adjacent nodes in the queue
Visited A B C D
Queue E G
Example-2
• Find the BFS traversal for the given node. Consider A
as Source node
Visited A B C D E
Queue G F
Example-2
• Find the BFS traversal for the given node. Consider A
as Source node
Visited A B C D E G
Queue F H I
Example-2
• Find the BFS traversal for the given node. Consider A
as Source node
Visited A B C D E G F
Queue H I
Example-2
• Find the BFS traversal for the given node. Consider A
as Source node
Visited A B C D E G F H
Queue I
Example-2
• Find the BFS traversal for the given node. Consider A
as Source node
Visited A B C D E G F H I
Queue
Visited A
Stack
TOP B
C
D
Example-2
• Find the DFS traversal for the given node. Consider A
as Source node
Visited A B
Stack
TOP E
C
D
Example-2
• Find the DFS traversal for the given node. Consider A
as Source node
Visited A B E
Stack
C
F
C
D
Example-2
• Find the DFS traversal for the given node. Consider A
as Source node
Visited A B E C
Stack
G
F
C
D
Example-2
• Find the DFS traversal for the given node. Consider A
as Source node
Visited A B E C G
F Stack
H
I
F
C
D
Example-2
• Find the DFS traversal for the given node. Consider A
as Source node
Visited A B E C G F
C
H
Stack
H
I
F
C
D
Example-2
• Find the DFS traversal for the given node. Consider A
as Source node
Visited A B E C G F H
E
I
Stack
H
I
F
C
D
Example-2
• Find the DFS traversal for the given node. Consider A
as Source node
Visited A B E C G F H
I
Stack
H
I
F
C
D
Example-2
• Find the DFS traversal for the given node. Consider A
as Source node
Visited A B E C G F H I
Stack
H
I
F
C
D
Example-2
• Find the DFS traversal for the given node. Consider A
as Source node
Visited A B E C G F H I
Stack
I
F
C
D
Example-2
• Find the DFS traversal for the given node. Consider A
as Source node
Visited A B E C G F H I
Stack
F
C
D
Example-2
• Find the DFS traversal for the given node. Consider A
as Source node
Visited A B E C G F H I
Stack
C
D
Example-2
• Find the DFS traversal for the given node. Consider A
as Source node
Visited A B E C G F H I
Stack
D
Example-2
• Find the DFS traversal for the given node. Consider A
as Source node
Visited A B E C G F H I D
Stack
D
• Space complexity The space complexity of a depth-first search is
lower than that of a breadth-first search.
• Time complexity The time complexity of a depth-first search is
proportional to the number of vertices plus the number of edges in
the graphs that are traversed.
• The time complexity can be given as (O(|V| + |E|)).
Applications of DFS
• Depth-first search is useful for:
• Finding a path between two specified nodes, u and v, of an
unweighted graph.
• Finding a path between two specified nodes, u and v, of a weighted
graph.
• Finding whether a graph is connected or not.
• Computing the spanning tree of a connected graph.