Professional Documents
Culture Documents
Examples:
1. Computing an (a > 0, n a nonnegative integer)
2. Computing n!
3. Multiplying two matrices
4. Searching for a key of a given value in a list
Brute-Force Sorting Algorithm
• Selection Sort
Void selection_sort( a, n )
{
int min, temp;
for (i=0;i<n-2;i++)
{
min = i;
for ( j = i+1; j < n-1; j++)
{
if (a[j] < a[min] )
min = j;
}
temp = a[ i ];
a[i] = a[min];
a[min] = temp;
}
}
Analysis:
C(n) = 0(n2)
Bubble Sort:
Brute-force algorithm
Step 1 Align pattern at beginning of text
Step 2 Moving from left to right, compare each character of
pattern to the corresponding character in text until
• all characters are found to match (successful search); or
• a mismatch is detected
Step 3 While pattern is not found and the text is not yet
exhausted, realign pattern one position to the right and
repeat Step 2
Pseudocode
Brute-Force Strengths and Weaknesses
• Strengths
• wide applicability
• simplicity
• yields reasonable algorithms for some important problems
(e.g., matrix multiplication, sorting, searching, string matching)
• Weaknesses
• rarely yields efficient algorithms
• some brute-force algorithms are unacceptably slow
• not as constructive as some other design techniques
Exhaustive Search
Method:
• generate a list of all potential solutions to the problem in a
systematic manner
• Given n cities with known distances between each pair, find the
shortest tour that passes through all the cities exactly once
before returning to the starting city
Given n items:
• weights: w1 w2 … wn
• values: v 1 v2 … v n
• a knapsack of capacity W
Find most valuable subset of the items that fit into the knapsack
Example 3: The Assignment Problem
There are n people who need to be assigned to n jobs, one
person per job. The cost of assigning person i to job j is C[i,j].
Find an assignment that minimizes the total cost.
Problem of size
“n”
Solution to the
original problem
Quick sort
This sorting technique works very well on large set of data.
Basically, quick sort works as follows,
The first step in this technique is to partition the given table into
two sub-tables based on Key element.
Such that the elements towards left of the key element are less
than the key element , towards right of the key element are
greater than key element.
T(n) = 0( n logn )
Merge Sort:
while ( j < n )
C[k] = B[j]
j = j+1;
k = k+1;
End while
In the above algorithm we considered two i/p arrays.
Suppose if we consider only single array (vector) and which is
divided into two sorted parts as shown below ,
Vector A
10 20 30 40 50 15 25 35 45
65 50 25 10 35 25 75 30
Conquering 1 50 65 10 25 25 35 30 75
2 10 25 50 65 25 30 35 75
3 10 25 25 30 35 50 65 75
Binary Search :
It is a simple searching technique which can be applied if
the items to be compared are either in ascending order or
descending order.
Method :
The program can be designed as follows,
if ( key == a[mid] )
{
pos = mid;
return;
}
Best-case analysis :
when the array is divided into half and if the key happens
to be A[mid], then no recursive calls are needed.
T ( n ) = O ( 1 ).
Worst-case analysis :
The worst-case occurs when either the key is not found in
the list or the key found as the first or the last element.
T ( n ) = C + T (n/2 )
.
T ( n ) = O ( log n ).
Binary Trees:
A binary is a tree which is a collection of zero or
more nodes. A tree can be empty or partitioned into three
subgroups namely root , left sub tree and right sub tree.
10
or or 10
10
20 12 20
Traversals :
Traversing is a method of visiting each node of a
tree exactly once in a systematic order.
During traversal, we may print the info field of
each node visited.
Preorder
Inorder
Post order
Preorder traversal:
a) Recursive technique.
b) Iterative procedure.
// Recursive function for preorder traversal.
a) Recursive technique.
b) Iterative procedure.
// Recursive function for Inorder traversal
a) Recursive technique.
b) Iterative procedure.
// Recursive function for Inorder traversal
B C
D
E F
G H
I
preorder : ABDEGHCFIJ
inorder : DBGEHACIFJ
preorder : ABDFGCEHJI
inorder : DBGEHACIFJ
Decrease and Conquer
Problem of size
“n”
Sub problem of
size “n-1”
Solution to the
original problem
The following are the applications discussed under this
strategy.
a) Insertion Sort
d) Topological sorting.
Graphs
A graph G consists of two things :
i.e., G = (V,E).
1. A set V of elements called nodes (or points or vertices)
v 3= { 1, 2, 3, 4, 5 }
1
E={(1,2),(1,5),(1,3),
5 4
(5,4),(4,3),(2,3)}
Graph Terminologies :
1 3 1 3
5 4 5 4
Path:
A Path is defined as a sequence of distinct vertices in
which each vertex is adjacent to the next.
Weighted graph :
It is the cost associated with each edge.
Degree of a node :
The degree of a node is the number of edges
incident on it.
In degree & out degree:
The in degree of a node n is the number of edges
that have n as the head.
The out degree of a node n is the number of edges
that have n as the tail.
Connected graph:
A connected graph is a graph in which path exists
b/w every pair of vertices.
Representation of a graph :
3 2
5 4
1 2 3 4 5
1 0 1 1 0 1
2 1 0 1 1 0
3 1 1 0 0 1
4 0 1 0 0 1
5 1 0 1 1 0
Graph traversal algorithms :
2 3
6 7
4 5
8
2 . Strongly connected, Directed graph :
1 2 1 2
3 4 3 4
Input Output
3 . Undirected, Disconnected graph :
5
5 6
1 2
1 2
6
4 3
4 3
7
8 7
Input output 1
8
output 2
4 . Directed weakly connected graph :
1 2 3
1 2
3
5 4
5 4
Input Output 1 output 2
In the above four cases, our algorithm “ dfs ” will work for
only example-1 and example-2.
to make it work the algorithm has to invoked for all
vertices.
Algorithm dfs visit_all ( G )
//input: G {V,E}
//Output: DFS spanning tree or forest.
for each vertex v € V do
visited [v] = 0//mark all vertices as unvisited.
for each vertex v € V do
if visited [v] = 0
dfs ( G , V )
end dfs visited_all
//C function for dfs
dfs (int v)
{
visited [v] = 1;
for( int w = 2; w <= n; w++ )
{
if(visited [w] == 0 && a[v][w] == 1)
{
printf(“%d”, w);
dfs(w);
}
}
}
//C function for dfs visit _all.
dfs_visit _all()
{
for (int i = 1; i <=n; i++)
{
visited[i] = 0;
}
for (int i = 1; i <= n; i++)
{
if (visited [i] == 0)
{
printf(“%d”, i)
dfs(i);
}
}
}
//Complete C program.
# include<stdio.h>
# define max=10;
// include dfs & dfs_visit_all functions
void main()
{
int a[max] [max];
int n;
printf(“enter the no of vertices”);
Scanf(“%d”, &n);
printf(“enter the adjacency matrix”)
for (int i=1; i<=n; i++)
for (int j=1; j<=n;j++)
Scanf(“%d”, &a[i][j]);
dfs_visit_all(); }
Check whether a given graph is connected or not using DFS method using C
programming.
#include<stdio.h>
#include<conio.h>
int a[20][20],reach[20],n;
void dfs(int v)
{ int i;
reach[v]=1;
for(i=1;i<=n;i++)
if(a[v][i] && !reach[i])
{
printf("\n %d->%d",v,i);
dfs(i);
}
}
int main()
{
int i,j,count=0;
//clrscr();
printf("\n Enter number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
reach[i]=0;
for(j=1;j<=n;j++)
a[i][j]=0;
}
for(i=1;i<=n;i++)
{
if(reach[i])
count++;
}
if(count==n)
printf("\n Graph is connected");
else
printf("\n Graph is not connected");
getch();
}
OUTPUT:
Enter number of vertices : 8
Enter the adjacency matrix :
0 1 1 0 0 0 0 0
1 0 0 1 1 0 0 0
1 0 0 0 0 1 1 0
0 1 0 0 0 0 0 1
0 1 0 0 0 0 0 1
0 0 1 0 0 0 0 1
0 0 1 0 0 0 0 1
0 0 0 1 1 1 1 0
Method :
A BFS starting at a vertex V first visits V, then it
visits all neighbors of V.
Then it visit all neighbors of the neighbors that have
not been visited before & so on.
Algorithm bfs (G, V)
2 3
6 7
4 5
8
// c function for bfs
bfs(int V)
{
f=0; r=-1;
visited[v] = 1;
insqueue(v);
while( f > r )
{
v = delqueue();
for ( int w = 1; w <= n; w++)
{
if(visited[w] == 0 && a[v][w] == 1)
{
insqueue(w);
printf("%d",w);
visited[w] = 1;
}
}
v = delqueue;
}
}
void insqueue( int e)
{
q[++r] = e;
}
int delqueue()
{
int t = q[f];
f++;
if( f > r)
{
f = 0;
r = -1;
}
return t;
}
//complete c program for bfs
#include <stdio.h>
#define max = 10;
int visited[max];
/* include the bfs, insqueue, delqueue functions.
void main()
{
int s; int i; int j; int n; a[10][10]
printf("Enter the no. of vertices");
scanf("%d", &n);
printf("Enter the adjacency matrix");
for(i = 1; i <= n; i++)
for( j =1; j <= n; j++)
scanf("%d",&a[i][j]);
for( i = 1; i<= n; i++)
visitied[i] = 0;
bfs(s);
}
TOPOLOGICAL SORTING :
Suppose you have list of tasks to do, some of
which depend on others to be completed first.
Condition:
Tasks 3 & 4 must be carried out only after task1. Task 3
must be completed before task 4.
Task 4 & 5 can be done after task 2.
Task 6 can be done only after all the rest have been
completed.
we can arrange this information in a dependency graph.
3
1
4 6
Topological orders,
2
1->3->4->2->5->6.
5
2->5->1->3->4->6.
Conditions to apply topological sorting :
a) using DFS
b) source removal method.
TOPOLOGICAL ORDER USING DFS :
then,
Reversing this order directly gives you the topological
ordering of a given dag.
E.g.
3
1
4 6
2
5
Topological orders,
First connected component : 1->3->4->6.
DFS is called again with 2 as start vertex,
Second connected component : 2->5.
void find_indegre()
{
int j,i,sum;
for(j=0;j<n;j++)
{ sum=0;
for(i=0;i<n;i++)
sum+=a[i][j];
indegre[j]=sum;
}
}
void topology()
{
int i,u,v,t[10],s[10],top=-1,k=0;
find_indegre();
for(i=0;i<n;i++)
{
if(indegre[i]==0) s[++top]=i;
}
while(top!=-1)
{
u=s[top--]; t[k++]=u;
for(v=0;v<n;v++)
{
if(a[u][v]==1)
{
indegre[v]--;
if(indegre[v]==0) s[++top]=v;
}
}
}
printf("The topological Sequence is:\n");
for(i=0;i<n;i++)
printf("%d ",t[i]);
}
void main()
{
int i,j;
clrscr();
topology(); getch();
}
ENTER NUMBER OF Nodes: 5
0 1 0 0 0
0 0 0 1 1
0 1 0 0 0
0 0 0 0 1
0 0 0 0 0
Algorithm Greedy ( a , n )
// a[1..n] contains “n” inputs.
{
solution = null.
for i = 1 to n do
{
x = select ( i );
if feasible ( solution , x ) then
solution = Union ( solution , x );
}
return solution;
}
// Minimum cost spanning trees :
Spanning tree :
or
a 1 b a 1 b a 1 b a 1 b
5 2 2 5 5 2
c d c d c d c d
3 3 3
Graph w(t1) = 6 w(t2) = 9 w(t3) = 8
// Minimum cost spanning trees :
a) Prim’s Algorithm.
b) Kruskala’s Algorithm.
Prim’s Algorithm.
Prim’s algorithm constructs a minimum spanning tree
through a sequence of expanding sub trees.
5
a b
1 4 6 2
3 7
c d e
Kruskal’s Algorithm.
The Kruskal's algorithm also uses the greedy criterion to
find the minimum cost spanning.
Method :
Sort E in nondecreasing order of the edge weights w(ei1) <= … <= w(ei|E|)
4
a b
3 2 5 6
7 4
c d e
4
b c
3 6
7 2 5 4
a d e
4
Tree vertices Remaining vertices b c
3 6
a(-,0) b(a,3) c(-,∞) d(a,7) e(-,∞) 2 5
a d e
7 4
4
b(a,3) c(b,3+4) d(b,3+2) e(-,∞) b c
3 6
2 5
a d e
7 4
4
d(b,5) c(b,7) e(d,5+4) b c
3 6
2 5
a d e
7 4
4
c(b,7) e(d,9) b c
3 6
2 5
a d e
7 4
e(d,9)
From a given vertex in a weighted connected graph, find
shortest paths to other vertices using Dijkstra's
algorithm (C programming).
#include<stdio.h>
#include<conio.h>
#define infinity 999
for(i=1;i<=n;i++)
{
flag[i]=0;
dist[i]=cost[v][i];
}
count=2;
flag[v]=1;
while(count<=n)
{
min=99;
for(w=1;w<=n;w++)
if(dist[w]<min&&!flag[w])
{
min=dist[w];
u=w;
}
flag[u]=1;
count++;
for(w=1;w<=n;w++)
if((dist[u]+cost[u][w]<dist[w]) && !flag[w])
dist[w]=dist[u]+cost[u][w];
}
}
void main()
{
int n,v,i,j,cost[10][10],dist[10];
clrscr();
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=infinity;
}
printf("\n Enter the source vertex:");
scanf("%d",&v);
dij(n,v,cost,dist);
printf("\n Shortest path:\n");
for(i=1;i<=n;i++)
if(i!=v)
printf("%d->%d,cost=%d\n",v,i,dist[i]);
getch();
}
OUTPUT:
0 3 999 7 999
3 0 4 2 999
999 4 0 5 6
7 2 5 0 4
999 999 6 4 0
Shortest path:
1 -> 2, cost = 3
1 -> 3, cost = 7
1 -> 4, cost = 5
1 -> 5, cost = 9
Find Minimum Cost Spanning Tree of a given
undirected graph using Kruskal's algorithm
( C programming)
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
void main()
{
clrscr();
printf("\n\tImplementation of Kruskal's algorithm\n");
printf("\nEnter the no. of vertices:");
scanf("%d",&n);
printf("\nEnter the cost adjacency matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
}
printf("The edges of Minimum Cost Spanning Tree
are\n");
while(ne < n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j <= n;j++)
{
if(cost[i][j] < min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
u=find(u);
v=find(v);
if(uni(u,v))
{
printf("%d edge (%d,%d) =%d\n",ne++,a,b,min);
mincost +=min;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tMinimum cost = %d\n",mincost);
getch();
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}