Algorithm Design Techniques
Yonglei Tao
Dept. of CS & IS
GVSU
Algorithm Design Techniques
 Divide and Conquer
 Greedy Method
 Graph Coloring Problem
 Backtracking
 Dynamic Programming
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
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
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
An Intersection
E
D
C
B
A
Note: roads C and E are one-way
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
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
Approach One
 Try all possibilities
 Works for a small graph
 Computationally expensive to find an optimal
solution for arbitrarily large graphs
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
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
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
AB AD
AC
DA DC
DB
EA EC
EB
BA BD
BC
ED
AB AD
AC
DA DC
DB
EA EC
EB
BA BD
BC
ED
AB AD
AC
DA DC
DB
EA EC
EB
BA BD
BC
ED
Notes on the Method
 Simple but might not produce an optimal
solution
 need to prove the result
1
4
3
5 2
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
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
}
}
}
// 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 );
}
}
// 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 );
// 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;
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
An Example
1
18
15
17
14
5
4
13
12
3
2 11
10
9
8
7
6
16
Backtracking Stack Operation
B1
3
2
1
B8
B9
5
4
B12
3
2
1
end
7
6
B8
B9
5
4
B12
3
2
1
end
8
B9
5
4
B12
3
2
1
end
11
10
9
5
4
B12
3
2
1
goal
16
15
14
B17
13
12
3
2
1
2
1
18
15
17
14
5
4
13
12
3
2 11
10
9
8
7
6
16
B1
3
2
1
B8
B9
5
4
B12
3
2
1
end
7
6
B8
B9
5
4
B12
3
2
1
end
8
B9
5
4
B12
3
2
1
end
11
10
9
5
4
B12
3
2
1
goal
16
15
14
B17
13
12
3
2
1
2
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
}
Four Queue Solution
Q
Q
Q
Q
1 2 3 4
1
2
3
4
Search Space
... ... ... ... ... ... ...
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
Computing Fibonacci Numbers
fib(5)
fib(4) fib(3)
fib(3) fib(2) fib(2) fib(1)
… … …
 Repeated recursive calls
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;
}

graph coloring.ppt

  • 1.
    Algorithm Design Techniques YongleiTao Dept. of CS & IS GVSU
  • 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 ColoringProblem  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
  • 6.
  • 7.
    Modeling  Highlight essentialfeatures 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  Coloringa 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  Tryall possibilities  Works for a small graph  Computationally expensive to find an optimal solution for arbitrarily large graphs
  • 10.
    Approach Two  Identifycertain 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  Changethe 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
  • 13.
    AB AD AC DA DC DB EAEC EB BA BD BC ED
  • 14.
    AB AD AC DA DC DB EAEC EB BA BD BC ED
  • 15.
    AB AD AC DA DC DB EAEC EB BA BD BC ED
  • 16.
    Notes on theMethod  Simple but might not produce an optimal solution  need to prove the result 1 4 3 5 2
  • 17.
    Step 1: InformalAlgorithm 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 voidColorGraph (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 Definitionof 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 implementationof 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 cleverimplementation 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
  • 23.
  • 24.
  • 25.
  • 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 }
  • 27.
  • 28.
    Search Space ... ...... ... ... ... ...
  • 29.
    Dynamic Programming  Solvea 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
  • 30.
    Computing Fibonacci Numbers fib(5) fib(4)fib(3) fib(3) fib(2) fib(2) fib(1) … … …  Repeated recursive calls
  • 31.
    An Iterative Function intfib (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; }