COMP2230 Introduction To Algorithmics: A/Prof Ljiljana Brankovic
COMP2230 Introduction To Algorithmics: A/Prof Ljiljana Brankovic
Lecture Overview
Searching Algorithms: Topological Sort text, Section 4.4 Backtracking text , Section 4.5
Searching
Topological Sort
Example: Given a list of courses and prerequisits, give a topological sort, that is a list of courses in order in which they can be taken to satisfy the prerequisites.
Topological Sort
Course 1 2 3 4 5 6 7 8 9 10 MATH130 MATH140 MATH200 MATH120 COMPSCI150 PHYS130 COMPSCI100 COMPSCI200 COMPSCI240 ENG110 Prerequisite MATH120 MATH130 MATH140, PHYS130 None MATH140 None MATH120 COMPSCI100, COMPSCI150, ENG110 COMPSCI200, PHYS130 none
Topological Sort
In order for topological sort to exist, there must not be a cycle in the prerequisites! We can model this problem as a directed graph it must not have any directed cycle directed acyclic graph (dag)
Topological Sort
1 4
2 5
10
Topological Sort
1 4
2 5
10
Topological Sort
1 4
2 5
10
Topological Sort
3
1 4 2 5 3
10
Topological Sort
3
1 4 2 5 3
10
Topological Sort
3
1 4 2 5 3
10
Topological Sort
9 3
1 4 2 5 3
10
Topological Sort
8 9 3
1 4 2 5 3
10
Topological Sort
5 8 9 3
1 4 2 5 3
10
Topological Sort
2 5 8 9 3
1 4 2 5 3
10
Topological Sort
1
1 4 2 5
2 5 8 9 3
3
10
Topological Sort
1
1 4 2 5
2 5 8 9 3
3
10
Topological Sort
7 1
1 4 2 5
2 5 8 9 3
3
10
Topological Sort
4 7 1
1 4 2 5
2 5 8 9 3
3
10
Topological Sort
6 4 7 1
1 4 2 5
2 5 8 9 3
3
10
Topological Sort
10 6 4 7 1
1 4 2 5
2 5 8 9 3
3
10
Input Parameters: adj Output Parameters: ts top_sort( adj, ts) { n = adj.last // k is the index in ts where the next vertex is to be // stored in topological sort. k is assumed to be global. k = n for i = 1 to n visit [i] = false for i = 1 to n if (! visit[v]) top_sort_recurs(adj,i ,ts) } top_sort_recurs(adj,start ,ts) { visit [start] = true trav = adj[start] while (trav != null) { v = trav. ver if (! visit[v]) top_sort_recurs(adj,v ,ts) trav = trav.next } ts[k] = start k = k - 1 }
Backtracking
Backtracking is basically a depth-first search applied to a dynamically generated tree. Example: The n-queens problem is to place n queens on an n n board so that no two queens are in the same row, column, or diagonal.
8 x 8 board
Example
x
x x
x x
x x
x x x x x
x x x x
n_queens( n) { rn_queens (1,n ) } rn_queens (k,n ) { for row[k ] = 1 to n if (position_ok(k ,n)) if (k == n) { for i = 1 to n print (row [i] + ) println() } else rn_queens (k + 1,n ) } position_ok(k ,n) for i = 1 to k - 1 // abs is absolute value if (row[k ] == row [i] || abs(row[k ] - row[ i]) == k - i ) return false return true }
Input Parameter: n Output Parameters: None n_queens( n) { for i = 1 to n row_used[ i] = false for i = 1 to 2 * n - 1 ddiag_used[i] = udiag_used[i] = false rn_queens (1,n ) } ...
... // When rn_queens (k,n ) is called, queens have been // properly placed in columns 1 through k - 1. rn_queens (k,n ) { for row[k ] = 1 to n if (position_ok(k ,n)) row_used[ row[ k]] = true ddiag_used[n - k + row[k]] = true udiag_used[k + row[k] - 1] = true if (k == n) { // Output a solution. Stop if only one // solution is desired. for i = 1 to n print (row [i] + ) println() } else rn_queens (k + 1,n ) row_used[ row[ k]] = false ddiag_used[n - k + row[k]] = false udiag_used[k + row[k] - 1] = false } ...
... // position_ok(k, n) returns true if the queen in column k // does not conflict with the queens in columns 1 // through k - 1 or false if it does conflict. position_ok(k ,n) return !( row_used [row [k]] || ddiag_used [n - k + row [k]] || udiag_used [k + row [k] - 1 ]) }
Total