You are on page 1of 20

Graph

● A collection of vertices and edges that connect these vertices.

● 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.

Graphs are used to represent many real life applications:

● Graphs are used to represent networks.


● Graphs are also used in social networks
● Null Graph: A graph having no edges is called a Null Graph.

● Simple Graph : A graph with no loops and no parallel edges is called a


simple graph.
○ The maximum number of edges possible in a single graph with ‘n’ vertices is nC2 where nC2
= n(n – 1)/2.

○ The number of simple graphs possible with ‘n’ vertices = 2nc2 = 2n(n-1)/2.
Directed and Undirected Graph
● 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.

● 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 regular graph of degree k.
● Path: A path P written as P = {v0, v1, v2,
..., vn), of length n from a node u to v is defined as
a sequence of (n+1) nodes. Here, u = v0, v = vn and vi–
1 is adjacent to vi for i = 1, 2, 3,..., n.

● Closed path: A path P is known as a closed path if the


edge has the same end-points. That is, if v0 = vn.

● Simple path: A path P is known as a simple path if all


the nodes in the path are distinct with an exception that
v0 may be equal to vn. If v0 = vn, 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).

● 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 to say that
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

● Wheel Graph: obtained from a cycle graph Cn-1 by adding a new


vertex. That new vertex is called a Hub which is connected to all
the vertices of Cn.
No. of edges in Wn = No. of edges from hub to all other vertices + No.
of edges from all other nodes in cycle graph without a hub.
= (n–1) + (n–1)
= 2(n–1)
● 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 C Õ V, such that for every two vertices in C, there is an edge that
connects two vertices.

● 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.
● 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.
● Bipartite Graph: A simple graph G = (V, E) with
vertex partition V = {V1, V2} is called a bipartite
graph if every edge of E joins a vertex in V1 to
a vertex in V2.

● Complete Bipartite Graph:


A bipartite graph ‘G’, G = (V, E) with partition V =
{V1, V2} is said to be a complete bipartite graph if
every vertex inV1 is connected to every vertex of
V2. In general, a complete bipartite graph
connects each vertex from set V1 to each vertex
from set V2.
Graph Representation: Way’s to store graph in computer memory

Sequential: adjacency matrix to store the mapping represented by vertices


and edges(nxn)
Traversal Techniques
● Breadth First Search (BFS)

1. Set status of each node as ready.


2. Insert nodes in queue the starting node and set
status as waiting. Loop until queue becomes empty
3. To delete start and set status= processed.
4. To insert all neighbors having status ready and set
them all waiting.
5. End loop
A
6. Exit
B C D

E F G

H I
Traversal Techniques
● Depth First Search (DFS)

1. Set status of each node as ready.


2. Push the Start node in stack and set status as
waiting. Loop until stack becomes empty
3. Pop node from stack and set status= processed.
4. Push all neighbors having status ready and set
them all waiting.
5. End loop
A
6. Exit
B C D

E F G

H I
Shortest Path Algorithm: Dijkstra’s Algorithm
100 b
1. Select the source node also called the initial node 80
2. Define an empty set N that will be used to hold
a 42
nodes to which a shortest path has been found.
c
3. Label the initial node with , and insert it into N. 15 20
4. Repeat Steps 5 to 7 until the destination node is in
N or there are no more labelled nodes in N. d 35
75 e
5. Consider each node that is not in N and is
connected by an edge from the newly inserted
node.
6. (a) If the node that is not in N has no label then SET
the label of the
node = the label of the newly inserted node + the b 80
length of the edge. a
(b) Else if the node that is not in N was already c
labelled, then SET its new label = minimum (label 15 20
of newly inserted vertex + length of edge, old label) d e
75
7. Pick a node not in N that has the smallest label
assigned to it and add it to N.
Prim’s Algorithm
100 b
Step 1: Select a starting vertex 80
Step 2: Repeat Steps 3 and 4 until there are fringe
a 42
vertices
c
Step 3: Select an edge e connecting the tree
15
vertex and fringe vertex that has minimum 20
weight d 35
Step 4: Add the selected edge and the vertex to 75 e
the minimum spanning tree T
[END OF LOOP]
Step 5: EXIT

● Tree vertices are a part of the minimum b 80


spanning tree T.
● Fringe vertices are currently not a part of T, a
but are adjacent to some tree vertex. c
● Unseen vertices are neither tree vertices 15 20
nor fringe vertices.
d
75 e
A
Kruskal’s Algorithm 7 1
6
5
Step 1: Create a forest in such a way that each B C D
graph is a separate tree. 8 4
3 5
Step 2: Create a priority queue Q that contains
all the edges of the graph. F G
2
Step 3: Repeat Steps 4 and 5 while Q is NOT
EMPTY

Step 4: Remove an edge from Q

Step 5: IF the edge obtained in Step 4 connects


two different trees, A
7 1
then Add it to the forest (for combining two trees
into one tree). B C D
ELSE Discard the edge 4
3
Step 6: END
F G
2
Time and Space
Complexity
Asymptotic notation

Big oh Big Omega Big Theta

● Upper Bond ● Lower Bond ( • Exact Time


(Atmost) ● Best Case Least) •Average Case
● Worst Case

O(c)/O(1)<O(log(log(n))<O(logn)<O(n1/2)
<O(n)<O(nlogn)<O(n2)<O(n3)<O(nk)<O(2n)<
O(nn)<O(22n)
Calculate time complexity( with for loop)

For (i=0;i<n;i++) For (i=0;i<n;i++)


{ {
// statement ------------------- n times For (j=0;j<i;j++)
} {
// statement ------------------- n*(n+1)/2 times
Complexity: O(n) }
}
For (i=0;i<n;i=i+2) Complexity: O(n2)
{ P=0
// statement ------------------- n/2 times For (i=1;p<=n;i++)
} {
p=p+1
Complexity: O(n) }
Complexity: O(√n)
For (i=0;i<n;i++)
{ For (i=1;i<=n;i=i*2)
For (j=0;j<n;j++) {
{ // statement ------------------- n/2 times
// statement ------------------- n*n times }
}
} Complexity: O(logn)
Complexity: O(n2)
Calculate time complexity(while and if)

i=0 While(m!=n)
While(i<n) {
{ If(m>n)
Statement {
i=i+1 m=m-n
} O(n) }
Else
{
N=n-m
i=1 }
While(i<n) }
{
Statement O(n)
i=i*2
} O( logn)
Space Complexity
= Input size + auxiliary memory
Function add(int a, int b) Function sum(int a[], int n)
{ {
sum = a+b; sum = 0;
return sum; For(i=0 to n)
} {sum=sum+a[i]}
return sum;
Space required: }
a= 4 byte
b=4 byte Space required:
sum =4 byte a= 4 byte*n
Auxiliary = 4 byte b=4 byte
total 16 bytes (constant space) sum =4 byte
Auxiliary = 4 byte
total 4n+12 bytes (linear
space)

You might also like