Documente Academic
Documente Profesional
Documente Cultură
Contents:
Brute Force and Exhaustive Search:
1. Selection Sort:
2. Bubble Sort:
3. Sequential Search:
4. Brute-force string matching:
5. Traveling Salesman Problem:
6. Knapsack Problem:
7. Assignment Problem:
8. DFS:
9. BFS.
Decrease and Conquer:
1. Insertion Sort:
2. Topological Sorting:
3. Binary Search.
1
Selection Sort
• Idea:
– Find the smallest element in the array
– Exchange it with the element in the first position
– Find the second smallest element and exchange it with
the element in the second position
– Continue until the array is sorted
• Disadvantage:
– Running time depends only slightly on the amount of
order in the file
2
Example
8 4 6 9 2 3 1 1 2 3 4 9 6 8
1 4 6 9 2 3 8 1 2 3 4 6 9 8
1 2 6 9 4 3 8 1 2 3 4 6 8 9
1 2 3 9 4 6 8 1 2 3 4 6 8 9
3
Selection Sort
Alg.: SELECTION-SORT(A)
n ← length[A] 8 4 6 9 2 3 1
for j ← 1 to n - 1
do smallest ← j
for i ← j + 1 to n
do if A[i] < A[smallest]
then smallest ← i
exchange A[j] ↔ A[smallest]
4
Analysis of Selection Sort
Alg.: SELECTION-SORT(A) cost times
n ← length[A] c1 1
for j ← 1 to n - 1 c2 n
do smallest ← j
≈n2/2 c3 n-1
n −1
for i ← j + 1 to n ∑ (n − j + 1)
comparisons
j =1
c4 n −1
do if A[i] < A[smallest] ∑ j =1
(n − j )
≈n
then smallest ← i c5 ∑
n −1
(n − j )
exchanges j =1
i
1 2 3 n
8 4 6 9 2 3 1
j
• Easier to implement, but slower than Insertion
sort
6
Example
8 4 6 9 2 3 1 1 8 4 6 9 2 3
i=1 j i=2 j
8 4 6 9 2 1 3 1 2 8 4 6 9 3
i=1 j i=3 j
8 4 6 9 1 2 3 1 2 3 8 4 6 9
i=1 j i=4 j
8 4 6 1 9 2 3 1 2 3 4 8 6 9
i=1 j i=5 j
8 4 1 6 9 2 3 1 2 3 4 6 8 9
i=1 j i=6 j
8 1 4 6 9 2 3 1 2 3 4 6 8 9
i=1 j i=7
j
1 8 4 6 9 2 3
7
i=1 j
Bubble Sort
Alg.: BUBBLESORT(A)
for i ← 1 to length[A]
do for j ← length[A] downto i + 1
do if A[j] < A[j -1]
i then exchange A[j] ↔ A[j-1]
8 4 6 9 2 3 1
i=1 j
8
Bubble-Sort Running Time
Alg.: BUBBLESORT(A)
c1
for i ← 1 to length[A]
c2
do for j ← length[A] downto i + 1
Comparisons: ≈ n2/2 c3
do if A[j] < A[j -1] c4
Exchanges: ≈ n2/2
n then exchangen A[j] ↔ A[j-1]n
T(n) = c1(n+1) + c2 ∑ (n − i + 1) + c3 ∑ (n − i) + c4 ∑ (n − i)
i =1 i =1 i =1
n
34
Exhaustive Search: TSP (contd.)
•
35
Traveling Salesman (TSP)
2
a b Consider only when b precedes c
5 3
8 7
c d
1
a! b! c! d! a 2+8+1+7 = 18
a! b! d! c! a 2+3+1+5 = 11 optimal
a! c! b! d! a 5+8+3+7 = 23
a! c! d! b! a 5+1+3+2 = 11 optimal
a! d! b! c! a 7+3+8+5 = 23
a! d! c! b! a 7+1+8+2 = 18
36
TSP code
#include<stdio.h>
int ary[10][10],completed[10],n,cost=0;
void takeInput()
{
int i,j;
printf("Enter the number of villages: ");
scanf("%d",&n);
printf("\nEnter the Cost Matrix\n");
for(i=0;i < n;i++)
{
printf("\nEnter Elements of Row: %d\n",i+1);
for( j=0;j < n;j++)
scanf("%d",&ary[i][j]);
completed[i]=0;
}
printf("\n\nThe cost list is:");
for( i=0;i < n;i++)
{
printf("\n");
for(j=0;j < n;j++)
printf("\t%d",ary[i][j]);
}
}
37
TSP code
void mincost(int city)
{
int i,ncity;
completed[city]=1;
printf("%d--->",city+1);
ncity=least(city);
if(ncity==999)
{
ncity=0;
printf("%d",ncity+1);
cost+=ary[city][ncity];
return;
}
mincost(ncity);
}
int least(int c)
{
int i,nc=999;
int min=999,kmin;
38
TSP code
for(i=0;i < n;i++)
{
if((ary[c][i]!=0)&&(completed[i]==0))
if(ary[c][i]+ary[i][c] < min)
{
min=ary[i][0]+ary[c][i];
kmin=ary[c][i];
nc=i;
}
}
if(min!=999)
cost+=kmin;
return nc;
}
int main()
{
takeInput();
printf("\n\nThe Path is:\n");
mincost(0); //passing 0 because starting vertex
printf("\n\nMinimum cost is %d\n ",cost);
return 0;
}
39
Exhaustive Search: Knapsack
problem
Ω(2n)
41
subset weight value
Knapsack problem (contd.)
Ø 0 $0
{1} 7 $42
{2} 3 $12
{3} 4 $40
W=10 w1 = 7
w2 = 3 {4} 5 $25
v1 = $42
v2 = $12 {1,2} 10 $54
W=50 w1 = 30
w2 = 20
v1 = $120 w3 = 10
v2 = $100
v3 = $60
knapsack Item 1 Item 2 Item 3
43
Knapsack algorithm
ALGORITHM Knapsack (Weights [1 ... N], Values [1 ... N],
Table [0 ... N, 0 ... Capacity])
//Input: ArrayWeightscontainstheweightsofallitems
Array Values contains the values of all items
Array Table is initialized with 0s; it is used to store the results from the dynamic programming algorithm.
// Output: The last value of array Table (Table [N, Capacity]) contains the optimal solution of the problem for the given Capacity
3
Use the i
item
for i = 0 to N do
for j = 0 to Capacity
if j < Weights[i] then
Table[i, j] Table[i-1, j]
else
Table[i, j] maximum { Table[i-1, j]
return Table[N, Capacity]
AND
Values[i] + Table[i-1, j – Weights[i]]
44
Exhaustive Search: Assignment Problem
45
Assignment problem (contd.)
Job 1 Job 2 Job 3 Job 4
Person 1 9 2 7 8
Person 2 6 4 3 7
Person 3 5 8 1 8
Person 4 7 6 9 4
Select one element in each row
Cost matrix so that all selected elements are
in different columns and total sum
of the selected elements is the smallest.
<j1, j2, …, jn> are feasible solution tuples
i-th component indicates the column of
the element selected in i-th row
e.g., <2, 3, 4, 1>
Generate all permutations of <1, 2, 3, 4>
And compute total cost, find the smallest cost.
46
Assignment problem (contd.)
9. 2 7 8
6. 4 3 7
C= Complexity is Ω(n!)
5. 8 1 8
7 6 9 4
And so on…
47
Exhaustive Search (contd.)
Problem domain
could be in the form
of a graph.
Then we have to traverse
the graph.
48
Graph Traversals
49
Depth-first search
• Start at a vertex, mark it as visited
• Go to one of unvisited neighbors, mark it visited, and
so on.
• At dead end, backs up one edge to the vertex it came
from and tries to visit unvisited vertices from there.
• Eventually halts after backing up to the starting
vertex, by then one connected component has been
traversed.
• If unvisited vertices remain, dfs starts at one of
them.
50
Depth-first Search (contd.)
g h
Tree edge
a e Back edge
c f
b Depth first search forest
d
j i
51
Depth-first Search (contd.)
52
Depth-first Search (contd.)
ALGORITHM: DFS(G)
// Input: Graph=<V, E>
mark each vertex in V with 0 as a mark of being “unvisited”
count <- 0
for each vertex v in V do
if v is marked with 0
dfs(v) Traversal time is in Θ(|V|2)
Or in Θ(|V| + |E|)
dfs(v)
count <- count+1; mark v with count Adjacency matrix
for each vertex w in V adjacent to v do
if w is marked with 0
Adjacency list
dfs(w)
53
Breadth-first Search
• Start at a vertex, mark it visited
• Go to each of its neighbors and put
them in a list
• Delete the starting vertex.
• Start at one of the remaining in the
list, mark it visited, and so on...
54
Breadth-first Search (contd.)
g h
Tree edges
a e Cross edges
c f
Breadth-first search forest
d b
j i
55
BFS (contd.)
ALGORITHM BFS(G)
mark each vertex in V with 0 as a mark of being “unvisited”
count <- 0
for each vertex v in V do Time complexity is similar
if v is marked with 0 to DFS
bfs(v) Gives shortest path between
bfs(v) two vertices
Count <- count+1; mark v with count and initialize a queue with v
while the queue is not empty do
for each vertex w in V adjacent to the front vertex do
if w is marked with 0
count <- count+1; mark w with count
add w to queue
remove the front vertex from the queue
56
Summary of DFS and BFS
DFS BFS
Data structure Stack Queue
Edge types Tree and back edges Tree and cross edges
Applications Connectivity, Connectivity,
Acyclicity, Acyclicity,
Articulation points Minimum-edge paths
Efficiency for Θ(|V|2) Θ(|V|2)
adjacency matrix
Efficiency for Θ(|V| + |E|) Θ(|V| + |E|)
adjacency list
57
Insertion Sort
• Idea: like sorting a hand of playing cards
– Start with an empty left hand and the cards facing
down on the table.
– Remove one card at a time from the table, and insert it
into the correct position in the left hand
• compare it with each of the cards already in the hand, from
right to left
– The cards held in the left hand are sorted
• these cards were originally the top cards of the pile on the
table
58
Insertion Sort
12
59
Insertion Sort
6 10 24 36
12
60
Insertion Sort
6 10 24 3
6
12
61
Insertion Sort
input array
5 2 4 6 1 3
sorted unsorted
62
Insertion Sort
63
INSERTION-SORT
Alg.: INSERTION-SORT(A) 1 2 3 4 5 6 7 8
a1 a2 a3 a4 a5 a6 a7 a8
for j ← 2 to n
do key ← A[ j ] key
i←j-1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i←i–1
A[i + 1] ← key
• Insertion sort – sorts the elements in place 64
Loop Invariant for Insertion Sort
Alg.: INSERTION-SORT(A)
for j ← 2 to n
do key ← A[ j ]
Insert A[ j ] into the sorted sequence A[1 . . j -1]
i←j-1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i←i–1
A[i + 1] ← key
Invariant: at the start of the for loop the elements in A[1 . . j-1]
are in sorted order
65
Analysis of Insertion Sort
INSERTION-SORT(A) cost times
for j ← 2 to n c1 n
do key ← A[ j ] c2 n-1
Insert A[ j ] into the sorted sequence A[1 . . j -1]
i←j-1 0 n-1
while i > 0 and A[i] > key c4 n-1
n
∑ j =2 t j
n
do A[i + 1] ← A[i] c5 ∑ j =2
(t j − 1)
n
i←i–1 ∑ (t j − 1)
c6 j =2
A[i + 1] ← key
c7
tj: # of times the while statement is executed at iteration j
n n cn 8 n-1
T (n) = c1n + c2 (n − 1) + c4 (n − 1) + c5 ∑ t j + c6 ∑ (t j − 1)+ c7 ∑ (t j − 1)+ c8 (n − 1)
j =2 j =2 j =2
66
Best Case Analysis
• The array is already sorted “while i > 0 and A[i] > key”
– A[i] ≤ key upon the first time the while loop test is run
(when i = j -1)
– tj = 1
= an + b = Θ(n) n n n
T (n) = c1n + c2 (n − 1) + c4 (n − 1) + c5 ∑ t j + c6 ∑ (t j − 1)+ c7 ∑ (t j − 1)+ c8 (n − 1)
j =2 j =2 j =2
67
Worst Case Analysis
• The array is in reverse sorted order “while i > 0 and A[i] > key”
– Always A[i] > key in while loop test
– Have to compare key with all elements to the left of the j-th
position ⇒ compare with j-1 elements ⇒ tj = j
n n n
n(n + 1) n(n + 1) n(n − 1)
using ∑ j= => ∑ j = − 1 => ∑ ( j − 1) = we have:
j =1 2 j =2 2 j =2 2
⎛ n( n + 1) ⎞ n( n − 1) n( n − 1)
T ( n ) = c1n + c2 ( n − 1) + c4 ( n − 1) + c5 ⎜ − 1⎟ + c6 + c7 + c8 ( n − 1)
⎝ 2 ⎠ 2 2
= an 2 + bn + c a quadratic function of n
70
Topological Sort
(an application of DFS)
Topological sort
• We have a set of tasks and a set of
dependencies (precedence constraints) of
form “task A must be done before task B”
• Topological sort: An ordering of the tasks that
conforms with the given dependencies
• Goal: Find a topological sort of the tasks or
decide that there is no such ordering
Examples
• Scheduling: When scheduling task graphs in
distributed systems, usually we first need to
sort the tasks topologically
...and then assign them to resources (the most
efficient scheduling is an NP-complete problem)
• Or during compilation to order modules/
libraries
d c
a g f
e
Examples
• Resolving dependencies: apt-get uses
topological sorting to obtain the
admissible sequence in which a set of
Debian packages can be installed/
removed
Topological sort more formally
• Suppose that in a directed graph G = (V, E)
vertices V represent tasks, and each edge (u,
v)∊E means that task u must be done before task
v
d=∞ d=∞
d e
f=∞ f=∞
d=∞ f
f=∞
Topological sort
1) Call DFS(G) to compute
Time = 3
2
the finishing times f[v]
d = 2∞ d=∞
d e
f=∞ f=∞
d=∞ f
f=∞
Topological sort
1) Call DFS(G) to compute
Time = 4
3
the finishing times f[v]
d=∞ 2) Let’s
as each vertex
say we is DFS
start the
f=∞ a finished,
from insert
the vertex c it onto
the front
Next of a linked
we discover the list
d=∞ d=1
b c f=∞ vertex d
f=∞ Next we discover the
vertex f
d=2 d=∞ f is done, move back to d
d e
f=∞ f=∞
d=3
3 f
f=∞
4
f
Topological sort
1) Call DFS(G) to compute
Time = 5
4
the finishing times f[v]
d=3 f
f=4
d f
Topological sort
1) Call DFS(G) to compute
Time = 6
5
the finishing times f[v]
d f
Topological sort
1) Call DFS(G) to compute
Time = 7
6
the finishing times f[v]
d=1 JustNext
a note: If there
we discover the was
d=∞
b c f=∞ vertex
(c,f) edged in the graph, it
f=∞ Next we discover the
would bef classified as a
vertex
d=2 d=6 f is done,
forward edgemove back to d
d e
f=5 f=7 (in this particular
d is done, DFS to
move back run)
c
d=3 f
f=4
c e d f
Topological sort
1) Call DFS(G) to compute
Time = 11
10
the finishing times f[v]
b c e d f
Topological sort
1) Call DFS(G) to compute
Time = 12
11
the finishing times f[v]
b c e d f
Topological sort
1) Call DFS(G) to compute
Time = 13
11
the finishing times f[v]
Let’sHAVE
now call DFSRESULT!
visit
d=9 WE THE
f = 12 a from the vertex a
3) return the linked list of
d=1 Next we discover the
d = 10 vertices
vertex c,
b c f=8
f = 11
but c was already
processed => (a,c) is a
d=2 d=6 Next
d e cross we
edgediscover the
f=5 f=7 vertex b
b is done as (b,d) is a cross
d=3 edge => now move back to
f c
f=4
a is done as well
a b c e d f
Topological sort
Time = 13
11
The linked list is sorted in
d=9 decreasing order of
f = 12 a finishing times f[]
d = 10 d=1
b c f=8 Try yourself with different
f = 11
vertex order for DFS visit
d=2 d=6
d e
f=5 f=7 Note: If you redraw the
graph so that all vertices
d=3 f are in a line ordered by a
f=4 valid topological sort, then
all edges point „from left
a b c e d f to right“
Time complexity of TS(G)
• Running time of topological sort:
Θ(n + m)
where n=|V| and m=|E|
• Why? Depth first search takes Θ(n + m)
time in the worst case, and inserting into
the front of a linked list takes Θ(1) time
Binary Search
O(log2 n)
left + right
mid =
2
Binary Search
bool BinSearch(double list[ ], int n, double item, int&index){
int left=0;
int right=n-1;
int mid;
while(left<=right){
mid=(left+right)/2;
if(item> list [mid]){ left=mid+1; }
else if(item< list [mid]){right=mid-1;}
else{
item= list [mid];
index=mid;
return true; }
}// while
return false;
}
Binary Search: Example
Binary Search
Binary Search