2. Algorithm Design Techniques
Divide and Conquer
Greedy Method
Graph Coloring Problem
Backtracking
Dynamic Programming
3. Divide and Conquer
Examples
Tree traversal algorithms
Quick sort, merge sort
Basic characteristics
A problem can be divided into sub-
problems of the same type
Solutions to sub-problems can be
combined
4. Greedy Method
Examples
Kruskal’s algorithm
Basic characteristics
Find an optimal solution to a problem
Solution cab be found in phases
Take what you can get now without regard for future
consequence
Hope that “local optimum” leads to “global
optimum”
Need proof
5. A Graph Coloring Problem
Write a program that helps design a traffic
light for a complicated intersection of roads
A set of permitted turns as the input
An optimal solution as the goal
Partition this set into as few groups as
possible
Associate a phase of the traffic light with
each group
7. Modeling
Highlight essential features while ignoring
irrelevant details
Model this problem using a graph
vertices represent turns
edges represent turns that cannot be performed
simultaneously
13 turns at the intersection on the previous
slide
8. Coloring Graph
Coloring a graph is to assign a color to each
vertex of the graph so that no two vertices
connected by an edge have the same color
The four color theory was originally posed as
a conjecture in the 1850s and finally proved
by two American mathematicians in 1976
9. Approach One
Try all possibilities
Works for a small graph
Computationally expensive to find an optimal
solution for arbitrarily large graphs
10. Approach Two
Identify certain special properties of a graph
Use them to eliminate some possibilities for
consideration
Hopefully, the number of possibilities we have
to look at are small enough
Not always possible to do
11. Approach Three
Change the goal a little and look for a good
but not necessarily optimal solution
may quickly produce a solution
then try to find out how good it is
done if it is good enough
Such an algorithm is called a heuristic
12. Using Greedy Method
A reasonable heuristic for the problem
One color at a time
To color as many vertices as possible
Coloring a vertex whenever allowed
without considering the potential
drawbacks in making such a move
16. Notes on the Method
Simple but might not produce an optimal
solution
need to prove the result
1
4
3
5 2
17. Step 1: Informal Algorithm
Repeat the following until all vertices in the given
graph are colored
// color as many as possible uncolored vertices
// of the graph with a new color
for each uncolored vertex v of g do
if v is not adjacent to any newly-colored vertex
mark v with the new color
18. Step 2: Pseudo-Code
void ColorGraph (GRAPH g, SET colored, SET newly_colored )
{
make newly_colored an empty set
for each uncolored vertex v of g do {
found = false
for each vertex w in newly_colored do
if v is adjacent to w
found = true
if not found {
// v is adjacent to no vertex in new_color
mark v colored
add v to newly_colored
}
}
}
19. // Step 3: C Code (using ADT LIST & GRAPH)
void ColorGraph ( GRAPH g, LIST colored, LIST newly_colored ) {
BOOLEAN found;
int v, // next vertex in colored
w, // next vertex in newly_colored
i, // position of v
j; // position of w
MakeEmpty ( newly_colored );
v = GetFirstVertex( g );
while ( v > 0 ) {
if ( ! Lookup ( v, colored ) ) {
found = FALSE; j = 0;
while ( ++j <= Length ( newly_colored ) ) {
w = Retrieve ( j, newly_clored );
if ( IsAdjacent ( w, v, g )
found = TRUE;
}
if ( ! found ) {
Insert ( v, colored );
Insert ( v, newly_colored );
}
}
v = GetNextVertex ( g );
}
}
20. // Type Definition of Boolean
typedef int BOOLEAN
#define FALSE 0
#define TRUE 1
// Basic operations for Abstract Data Type List
void MakeEmpty ( LIST * pL );
void Insert ( int x, LIST * pL );
void Delete ( int x, LIST * pL );
int Retrieve ( int p, LIST L );
int Length ( LIST L );
BOOLEAN Lookup ( int x, LIST L );
21. // Linked-list implementation of List
typedef struct CELL * LIST
struct CELL {
int element;
LIST next;
};
// Array implementation of LIST
#define MAX 100
typedef struct {
int A[MAX];
int length;
} LIST;
22. Backtracking
A clever implementation of exhaustive search
With generally unfavorable performance
In some cases savings over a brute force
exhaustive search can be significant
Examples
The eight queens problem
Computer gaming, decision analysis, and expert
systems
May create an application framework
26. void findGoal ( Node start ) { // assume start exists and
create a stack // the goal can be found
current = start
while ( current is not the goal ) {
push (current, stack)
if ( current is a branch point ) {
while ( more child nodes to process ) {
make the child
push ( child, stack );
}
unmark the top node
current = top(stack)
}
if ( current is end ) {
while ( top(stack) is unmarked ) pop (stack)
unmake the top node
current = top(stack)
}
advance current to the next node
}
push (current, stack) // push the goal onto stack
print path to the goal from start using stack
}
29. Dynamic Programming
Solve a problem by identifying a set of sub-
problems and tackling them one by one
Smallest first
Using the answers to small problems to help
work out larger ones
Record answers in a table
31. An Iterative Function
int fib (int n) {
if ( n <= 1)
return 1;
int last = 1, nextToLast = 1, answer = 1;
for (int i = 2; i <= n; i++) {
answer = last + nextToLast;
nextToLast = last;
last = answer;
}
return answer;
}