Sunteți pe pagina 1din 99

UNIT 2:

ALGORITHM DESIGN TECHNIQUES

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

exchange A[j] ↔ A[smallest] c6


n −1 n −1 n −1
T ( n) = c1 + c2 n + c3 ( n − 1) + c4 ∑ ( n − j + 1) + c5 ∑ (n − j ) + c6 ∑ (n − j ) + c7 ( n − 1) = Θ( n 2 ) 5
j =1 j =1 j =2
Bubble Sort
• Idea:
– Repeatedly pass through the array
– Swaps adjacent elements that are out of order

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

= Θ(n) + (c2 + c2 + c4) ∑ (n − i )


i =1
n n n 2
n ( n + 1) n n
where ∑ (n − i ) =∑ n − ∑ i = n 2 − = −
i =1 i =1 i =1 2 2 2
Thus,T(n) = Θ(n2) 9
The Sequential Search

(Linear Search)
The Sequential Search

Description
The Sequential (or Linear) Search examines
the first element in the list and then
examines each “sequential” element in the
list (in the order that they appear) until a
match is found. This match could be a
desired word that you are searching for, or
the minimum number in the list.
The Sequential Search

Variations
Variations on this include: searching a sorted list
for the first occurrence of a data value, searching a
sorted list for all occurrences of a data value (or
counting how many matches occur: inventory), or
searching an unsorted list for the first occurrence
or every occurrence of a data value.
You may indicate that a match has been found, the
number of matches that have been found, or the
indices where all the matches have been found.
Sequential Search Algorithm
Set index to 0
while index < length
if list[index] is equal to target then
return index
else
Increment the index by 1
return -1
Sequential Search C + +
int search(int target, const apvector<int> &v)
{
int index = 0;
while (index < v.length())
if (v[index] = = target)
return index;
else
++index;
return -1;
}
A Sequential Search C + +
int Sequential_Search(int target, apvector <int> &list, int length)
{
int index = 0;
bool found = false;
while((index < length) && ! found)
if (list[index] = = target)
found = true;
else
++index;
if (found)
return index;
else
return -1
}
A Sequential Search Example
Target ? 6
2
We start by searching for the 1
target at the first element in the 3
List and then proceed to
examine each element in the 5
order in which they appear. 4
A Sequential Search Example
6
Target ? 2
1
3
5
4
A Sequential Search Example
6
2
Target ? 1
3
5
4
A Sequential Search Example
6
2
1
Target ? 3
5
4
A Sequential Search Example
Once the target data item has 6
been found, you may return a
Boolean true, or the index 2
where it was found. 1
3
Target ! 5
4
Big - O Notation
Big - O notation is used to describe the efficiency
of a search or sort. The actual time necessary to
complete the sort varies according to the speed of
your system. Big - O notation is an approximate
mathematical formula to determine how many
operations are necessary to perform the search or
sort. The Big - O notation for the Sequential
Search is O(n), because it takes approximately n
passes to find the target element.
String Matching
Pattern Matching
Algorithms
The Problem
• Given a text T and a pattern P, check
whether P occurs in T
– eg: T = {aabbcbbcabbbcbccccabbabbccc}
– Find all occurrences of pattern P = bbc
• There are variations of pattern matching
– Finding “approximate” matchings
– Finding multiple patterns etc..
Why String Matching?
• Applications in Computational Biology
– DNA sequence is a long word (or text) over a 4-letter alphabet
– GTTTGAGTGGTCAGTCTTTTCGTTTCGACGGAGCCCCCAATTA
ATAAACTCATAAGCAGACCTCAGTTCGCTTAGAGCAGCCGAAA
…..
– Find a Specific pattern W
• Finding patterns in documents formed using a large alphabet
– Word processing
– Web searching
– Desktop search (Google, MSN)
• Matching strings of bytes containing
– Graphical data
– Machine code
• grep in unix
– grep searches for lines matching a pattern.
String Matching
• Text string T[0..N-1]

T = “abacaabaccabacabaabb”
• Pattern string P[0..M-1]

P = “abacab”
• Where is the first instance of P in T?

T[10..15] = P[0..5]
• Typically N >>> M
String Matching
abacaabaccabacabaabb • The brute force algorithm
abacab • 22+6=28 comparisons.

abacab
abacab
abacab
abacab
abacab
abacab
abacab
abacab
abacab
abacab
Naïve Algorithm

(or Brute Force)
• Assume |T| = n and |P| = m
Text T
Pattern P
Pattern P
Pattern P

Compare until a match is found. If so return the index where match


occurs
else return -1
Brute Force Version 1
static int match(char[] T, char[] P){
for (int i=0; i<T.length; i++){
boolean flag = true;
if (P[0]==T[i])
for (int j=1;j<P.length;j++)
if (T[i+j]!=P[j])
{flag=false; break;}
if (flag) return i;
}
}
• What is the complexity of the
code?
A bad case
00000000000000001
• 60+5 = 65
0000- comparisons are
0000- needed
0000- • How many of them
0000- could be avoided?
0000-
0000-
0000-
0000-
0000-
0000-
0000-
0000-
00001
A bad case
00000000000000001
• 60+5 = 65
0000- comparisons are
0000- needed
0000- • How many of them
0000- could be avoided?
0000-
0000-
0000-
0000-
0000-
0000-
0000-
0000-
00001
Typical text matching
This is a sample sentence
-
- • 20+5=25
-
s- comparisons are
- needed
-
s- (The match is near the same
- point in the target string as
- the previous example.)
- • In practice, 0≤j≤2
s-
-
-
-
-
-
-
sente
String Matching
• Brute force worst case
– O(MN)
– Expensive for long patterns in repetitive text
• How to improve on this?
• Intuition:
– Remember what is learned from previous
matches
Exhaustive Search
• Traveling Salesman Problem (TSP)
– Find the shortest tour through a given set of
n cities that visits each city exactly once
before returning to the city where it started
– Can be conveniently modeled by a weighted
graph; vertices are cities and edge weights
are distances
– Same as finding “Hamiltonian Circuit”: find a
cycle that passes through all vertices exactly
once

34
Exhaustive Search: TSP (contd.)
•  

How can we solve TSP?

Get all tours by generating all permutations


of n-1 intermediate cities, compute the tour
lengths, and find the shortest among them.

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

• Given n items of weights w1, w2, …, wn and


values v1, v2, …, vn and a knapsack of
capacity W, find the most valuable subset
of the items that fit into the knapsack
– A transport plane has to deliver the most
valuable set of items to a remote location
without exceeding its capacity

How can we solve it ?


40
Knapsack problem (contd.)

• Brute force: Generate all possible


subsets of the n items, compute total
weight of each subset to identify
feasible subsets, and find the subset
of the largest value
What is the time efficiency ?

Ω(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

knapsack Item 1 Item 2 {1,3} 11 !feasible


{1,4} 12 !feasible
How about
w4 = 5 {2,3} 7 $52
taking items w3 = 4
v4 = $25 {2,4} 8 $37
in decreasing order v3 = $40
of value/weight? {3,4} 9 $65
Item 3 Item 4
{1,2,3} 14 !feasible
{1,2,4} 15 !feasible
Works for this example! ☺
{1,3,4} 16 !feasible
42
{2,3,4} 12 !feasible
Knapsack problem (contd.)

W=50 w1 = 30
w2 = 20
v1 = $120 w3 = 10
v2 = $100
v3 = $60
knapsack Item 1 Item 2 Item 3

Item 1: $4/unit {Item3, Item2} = $60+$100 = $160


Item 2: $5/unit {Item3, Item1} = $60+$120 = $180
Item3: $6/unit
{Item2, Item1} = $100+$120 = $220

Doesn’t work for this example ☹

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

• There are n people who need to be


assigned to execute n jobs, one person
per job.
• C[i, j] : cost that would accrue if i-th
person is assigned to j-th job.
• Find an assignment with the minimum
total cost.

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

Efficient algo exists < 1, 2, 3, 4 > cost = 9+4+1+4 = 18


Called the “Hungarian
method”. < 1, 2, 4, 3 > cost = 9+4+8+9 = 30

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

• Problem domain can be a graph


• Exhaustive search needs to visit each
vertex and do something at it
• Two important graph-traversal
algorithms: depth-first search,
breadth first search

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

Which data structure to use ?

51
Depth-first Search (contd.)

How to decide if the graph is connected?


Start a DFS traversal at an arbitrary vertex
and check, after the traversal halts, whether
all the vertices of the graph will have been visited.

How to decide if the graph acyclic?


If no back edges, acyclic.

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

Which data structure to use ?

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

To insert 12, we need to


make room for it by moving
first 36 and then 24.
6 10 24 36

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

at each iteration, the array is divided in two sub-arrays:

left sub-array right sub-array

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

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
• 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

• T(n) = c1n + c2(n -1) + c4(n -1) + c5(n -1) + c8(n-1) =

(c1 + c2 + c4 + c5 + c8)n + (c2 + c4 + c5 + c8)

= 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

• T(n) = Θ(n2) order


n of growth
n in n 2
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 68
Comparisons and Exchanges in 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]
0 n-1
i←j-1
≈n /2 comparisons
2
while i > 0 and A[i] > key c4 n-1n
∑ j =2 j
t
do A[i + 1] ← A[i] c5 n
(t j − 1)
∑ j =2
i←i–1 n
≈n
2
/2 exchanges c6 ∑ j =2
(t j − 1)
A[i + 1] ← key
c7
69
Insertion Sort - Summary
• Advantages
– Good running time for “almost sorted” arrays Θ(n)
• Disadvantages
– Θ(n2) running time in worst and average case
– ≈ n2/2 comparisons and exchanges

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

• What is an ordering of vertices 1, ..., |V| such


that for every edge (u, v), u appears before v in
the ordering?

• Such an ordering is called a topological sort of G


• Note: there can be multiple topological sorts of G
Topological sort more formally
• Is it possible to execute all the tasks in G in an
order that respects all the precedence
requirements given by the graph edges?
• The answer is "yes" if and only if the directed
graph G has no cycle!
(otherwise we have a deadlock)
• Such a G is called a Directed Acyclic Graph, or
just a DAG
Algorithm for TS
• TOPOLOGICAL-SORT(G):
1) call DFS(G) to compute finishing times f[v]
for each vertex v
2) as each vertex is finished, insert it onto the
front of a linked list
3) return the linked list of vertices

• Note that the result is just a list of


vertices in order of decreasing finish
times f[]
Back to topological sort
• TOPOLOGICAL-SORT(G):
1) call DFS(G) to compute finishing times f[v]
for each vertex v
2) as each vertex is finished, insert it onto the
front of a linked list
3) return the linked list of vertices
Topological sort
1) Call DFS(G) to compute
Time = 2
1
the finishing times f[v]

d=∞ Let’s say we start the DFS


f=∞ a from the vertex c

d = 1∞ Next we discover the


d=∞
b c f=∞ vertex d
f=∞

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=∞ Let’s say we start the DFS


f=∞ a from the vertex c

d=1 Next we discover the


d=∞
b c f=∞ vertex d
f=∞

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=∞ Let’s say we start the DFS


f=∞ a from the vertex c

d=1 Next we discover the


d=∞
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=5 f=∞
d is done, move back to c

d=3 f
f=4

d f
Topological sort
1) Call DFS(G) to compute
Time = 6
5
the finishing times f[v]

d=∞ Let’s say we start the DFS


f=∞ a from the vertex c

d=1 Next we discover the


d=∞
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=5 f=∞
d is done, move back to c

d=3 f Next we discover the


f=4 vertex e

d f
Topological sort
1) Call DFS(G) to compute
Time = 7
6
the finishing times f[v]

d=∞ Let’s say we start the DFS


f=∞ a from the vertex c

d=1 Next we discover the


d=∞
b c f=∞ vertex d
f=∞ Next we discover the
Both
vertexedges
f from e are
d=2 d=6 f is done, move back to d
f=5
d e
f=∞
cross edges
d is done, move back to c

d=3 f Next we discover the


f=4 vertex e
e is done, move back to c
e d f
Topological sort
1) Call DFS(G) to compute
Time = 8
7
the finishing times f[v]

d=∞ Let’s say we start the DFS


f=∞ a from the vertex c

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 Next we discover the


f=4 vertex e
e is done, move back to c
c e d f c is done as well
Topological sort
1) Call DFS(G) to compute
Time
Time==10
9
the finishing times f[v]

d = 9∞ Let’s now call DFS visit


f=∞ a from the vertex a

d=1 Next we discover the


d=∞
b c f=8 vertex c,
f=∞
but c was already
processed => (a,c) is a
d=2 d=6 Next
d e cross we discover the
edge
f=5 f=7 vertex b

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]

d=9 Let’s now call DFS visit


f=∞ a from the vertex a

d=1 Next we discover the


d = 10
b c f=8 vertex c,
f = 11

but c was already
processed => (a,c) is a
d=2 d=6 Next
d e cross we discover the
edge
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

b c e d f
Topological sort
1) Call DFS(G) to compute
Time = 12
11
the finishing times f[v]

d=9 Let’s now call DFS visit


f=∞ a from the vertex a

d=1 Next we discover the


d = 10
b c f=8 vertex c,
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

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)


● A binary search looks for an


item in a list using a divide-and-
conquer strategy
Binary Search
– Binary search algorithm assumes that the items in the
array being searched are sorted
– The algorithm begins at the middle of the array in a
binary search
– If the item for which we are searching is less than the
item in the middle, we know that the item won’t be
in the second half of the array
– Once again we examine the “middle” element
– The process continues with each comparison cutting
in half the portion of the array where the item might
be
Binary Search
Binary Search: middle element

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

S-ar putea să vă placă și