Professional Documents
Culture Documents
○ 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.
E F G
H I
Traversal Techniques
● Depth First Search (DFS)
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
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)
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)