Algorithms
Algorithms
v1
/ \
e1/ \e3
/ \
v2 ------ v3
e2
Directed graph
edges are directed or "one way"
edges are ordered pairs
order of vertices is important for describing the edge
also called diagraph
Degree of graph
For undirected graph:
self-loop=2 degrees counts
otherwise degree=number of edges incident with a vertex.
degree for each vertex is calculated.
Directed graph:
there are in and out degree.
in=towards the vertex
out=outside of vertex
Types of graph
regular graph
the degree of all vertices is same
that same degree is called regularity of the graph
and the graph is referred to as regular graph
complete graph
All vertices connected with each other(all are adjacent to each other)
null graph
if edges is empty, then it's null graph
bipartite graph
A graph is bipartite iff the vertices can be partitioned into two sets such that there is no
edge between any pair of vertices in the same set.
complete bipartite graph
Graph isomorphism
The two graphs G1 and G2 are said to be isomorphic graphs if it's possible to redraw any G1
using G2 or G2 using G1.
Properties of isomorphic graphs:
Special graphs
weighted graphs
If a graph has a number associated with an edge(called weight), it's called weighted
graph.
walk, trail, path and circuits
walk: alternating list of vertices and edges
trail: walk with no repeated edges.
Circuit: A closed trail is a circuit.
Cycle: Circuit with no repeated vertex is called cycle.
path: walk with no repeated vertex.
connected graphs, disconnected graphs and components
A graph is connected if every vertex is joined to every other vertex by a path. A
disconnected graph is a graph that is not connected. Think of the components of a
graph as “connected pieces” of the graph that are disconnected from each other. A
connected graph has only one component, which is the entire graph
euler path:
visit all edges exactly only once.
Hamiltonian paths and Hamiltonian circuits
Representation of graph
Adjacency Matrix for undirected graph
v1 v2 v3 v4 v5
v1 0 1 1 1 0
v2 1 0 1 0 1
v3 1 1 0 1 1
v4 1 0 1 0 1
v5 0 1 1 1 0
The value 1 in the matrix means that there an edge exists between two vertices.
Diagonal elements has value zero generally (unless self-loops).
Adjacency Matrix of an undirected graph is symmetric.
Incidence Matrix
Indicates which edges occur at each vertex. This matrix is drawn for the graph shown below.
AB AC BC CD
A 1 1 0 0
B 1 0 1 0
C 0 1 1 1
D 0 0 0 1
Cost adjacency matrix
Weighted graphs are represented by cost adjacency matrix. If there is a edge between two
vertices, instead of placing "1", the weight of edge is placed.
If there's no connection, instead of placing "0", infinity is placed.
Linked-adjacency matrix
For this matrix will be represented as.
https://old.reddit.com/r/ObsidianMD/comments/1iqhmco/how_to_draw_that_linked_representati
onim_using/
connected
if there is a path between every pair of vertices
path=alternating sequences of edge and vertices?
weighted
undirected
They're not acyclic.
Tree
connected
undirected
acyclic
graph
Graph-->Subgraph
When we remove edges from a connected, weighted, undirected graph G to form a subgraph
such that connectivity is still satisfied & the sum of the weights on the remaining edges is as
small as possible.
Such a subgraph must be a tree.
Example: Take a cycle [v3,v4,v5,v3] , if we remove v4 & v5 , it'd still be connected graph &
could have even lesser minimum weight.
Spanning tree
is a connected subgraph
contains all the vertices in G
and is a tree.
Prim's algorithm
“Algorithms-1740123188608.png” could not be found.
This graph can be represented as
v1 v2 v3 v4 v5
v1 0 1 3 ∞ ∞
v2 1 0 3 6 ∞
v3 3 3 0 4 2
v4 ∞ 6 4 0 5
v5 ∞ ∞ 2 5 0
Y={v1}
while(the instance is not solved){
select a vertex in (V-Y) that is nearest to Y;
add the vertex to Y;
if(Y==V)
the instance is solved;
}
Kruskal's algorithm
Take the same graph as above.
Sort the edges in increasing order of weight.
Keep combining them one after another without forming a loop.
Dijkstra's shortest path
“Algorithms-1740315940677.png” could not be found.
Iteration 1: We see the smallest path is (2). So, now traverse everything that can be reached
from 2 directly in next iteration.
3/(1) is read as distance of 3 from 1.
Iteration 3: Note: (3)(This is a vertex that's why enclosed within parentheses).
The shortest path in iteration 2nd (besides the already visited vertex (2)) is (3).
Thus, the shortest path is (1)(2)(3)(5)(6) and it's of length 12.
Initialization of bellman-ford
d(s) = 0
All the shortest paths to all nodes except the source are set to infinity.
Relaxation
After initialization, every edge is considered for relaxation. Relaxation means to check whether
the path that is pointed by the edge can be shortened.
if d(u)+cost(u, v) < d(v)
d(v) = d(u) + cost(u, v)
After first iteration, shortest path among the paths from s to all its immediate neighbors that are
one hop away is updated.
After second iteration, all the vertices that are connected to s by two hops are updated.
This process continues n − 1 times.
v1 v2 v3 v4 v5
d 0 nil nil nil nil
pi / / / / /
Iteration 02:
Relax every edge one hop from v1.
i.e.
Relax v1v2, v1v3, v1v4.
v1 v2 v3 v4 v5
d 0 3 4 1 nil
pi / v1 v1 v1 /
Iteration 03:
Relax every edge two hop away from v1.
i.e. Relax v2v3,v2v5,v3v4,v4v5.
v1 v2 v3 v4 v5
d 0 3 -4 -3 3
pi / v1 v2 v3 v4
Transitive closure
It finds out all paths in a graph. i.e. the connectivity of a graph.
The meaningful question about connectivity is: What vertices can you reach if you start on a
particular vertex?
Warshall's algorithm
If you can get from vertex L to vertex M, and you can get from M to N, then you can get from L
to N.
Step 01: Draw the adjacency matrix
A B C D E
A 0 0 1 0 0
B 1 0 0 0 1
C 0 0 0 0 0
D 0 0 0 0 1
E 0 0 1 0 0
Row A
Go to each column till you find 1.
Found 1 at C.
A->C there's a path.
That means if X->A exists, then X->C exists by transitivity.
To find X->A exists, see column A and find a value which is 1.
B->A is 1.
It means B->A exists, A->C exists thus B->C exists as well.
A B C D E
A 0 0 1 0 0
B 1 0 01 0 1
C 0 0 0 0 0
D 0 0 0 0 1
E 0 0 1 0 0
Row B
Go to each column till you find 1.
B->A there's a path.
That means if X->B exists, then X->A exists by transitivity.
To find X->B exists, see column B and find a value which is 1. There's no 1 in B.
Move ahead.
B->C there's a path.
But since there's no path from X->B, we don't need to check any further.
Row C
No path
Row D
D->E exists.
That means if X->D exists, then X->E exists by transitivity.
To find whether X->D exists, see column D and find a value which is 1. None. Thus skip.
Row E
E->C exists.
That means if X->E exists, then X->C exists by transitivity.
To find whether X->E exists, see column E and find a value which is 1. It's B and D
D->E exists
E->C exists
Thus D->C exists.
B->E exists
E->C exists
B->C exists (it's already been generated).
Tree
hierarchical relationship is best explained by "tree".
non-linear data structure.
1 L
2 M
3 N
4 O
1 L
5 P
6 Q
7 R
8 S
9 T
10 U
11 V
12 W
13 X
14 Y
15 Z
To find information of node with index i , in the sequential representation, use the formulas
given below.
The parent of i is i/2 . For example, the parent of O is M.
The left child of i is 2i . For example, the left child of 7 (R) is Y(14). If 2i>n , there's no left
child
The right child of i is 2i+1 . For example, the right child of 7(R) is 15(Z). If 2i+1>n , there's no
left child.
Tree traversal
pre-order
1st root visit, then left subtree then right subtree
L,M,N
in-order
1st left hand subtree, then root, then right hand subtree
M,L,N
post-order
1st left subtree, then right subtree, then root
M,N,L
if(tree is empty)
return not found
if(root node=search target)
return found
if(search target<root)
return btsearch(left-child,search target)
if(search target>root)
return btsearch(right-child, search target)
20<66
Move left of root node
20<40
Move left of node 40
20<30
Move left of node 30
Place it there.
“Algorithms-1740749181687.png” could not be found.
33: 0
35: 1
47: 0
40:0
50: 3-1=2
50 node is problematic for well balanced tree. To restore balance, right rotation on node 50.
50 goes towards 63
40 goes towards 50
35 goes towards 40
33 goes towards 35
47 is orphaned
Huffman algorithm
Character Frequency
a 16
b 5
c 12
d 17
e 10
f 25
Now remove b & e from the priority queue & add n1 to it.
c,12
n1,15
a,16
d,17
f,25
Remove n1,c from the priority queue & add n2 to the priority queue(And sort it as well).
a,16
d,17
f,25
n2,27
Remove f,n2 from priority queue like this. Add n4,52 instead.
n3,33
n4,52
Insertion sort
30,25,15,20,28
Assume 30 is sorted and rest is unsorted
Insert 25 into sorted array. And then sort it.
25,30|15,20,28
Insert 15 into sorted array. And then sort it.
15,25,30|20,28
Insert 20 into sorted array. And then sort it.
15,20,25,30|28
Insert 28 into sorted array. And then sort it.
15,20,25,28,30
Selection sort
7|4|3|6|5
Iteration 01:
7 vs 4 --> 4
4 vs 3 --> 3
3 vs 6 --> 3
3 vs 5 --> 3
Smallest=3
Swap 3 with 7
3,4,7,6,5
Iteration 02:
4 vs 7 --> 4
4 vs 6 --> 4
4 vs 5 --> 5
Smallest=4
Keep as it is
3,4,7,6,5
Iteration 03:
7 vs 6 --> 6
6 vs 5 --> 5
Swap 7 and 5
3,4,5,6,7
Iteration 04:
6 vs 7 --> 6
Sorted finally.
Merge sort
Shell sort
Sort this:
34,12,22,09,04,60,56,14
Soln:
Take gap=4,
34|xx|xx|xx|04|xx|xx|xx
xx|12|xx|xx|xx|60|xx|xx
xx|xx|22|xx|xx|xx|56|xx
xx|xx|xx|09|xx|xx|xx|14
Sort it line by line
04|xx|xx|xx|34|xx|xx|xx
xx|12|xx|xx|xx|60|xx|xx
xx|xx|22|xx|xx|xx|56|xx
xx|xx|xx|09|xx|xx|xx|14
Take gap=4/2
04|xx|22|xx|34|xx|56|xx
xx|12|xx|09|xx|60|xx|14
xx|xx|xx|xx|xx|xx|xx|xx
xx|xx|xx|xx|xx|xx|xx|xx
Sort it line by line.
04|xx|22|xx|34|xx|56|xx
xx|09|xx|12|xx|14|xx|60
xx|xx|xx|xx|xx|xx|xx|xx
xx|xx|xx|xx|xx|xx|xx|xx
Take gap=2/2
And apply insertion sort
Write the sorted array as the answer.
Heap sort
Heap is essentially complete binary tree, such that:
if(child>parent)
interchange child, parent
Go to next parent(i/2)