SlideShare a Scribd company logo
1 of 31
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;
}

More Related Content

Similar to graph coloring.ppt

Jarrar.lecture notes.aai.2011s.ch3.uniformedsearch
Jarrar.lecture notes.aai.2011s.ch3.uniformedsearchJarrar.lecture notes.aai.2011s.ch3.uniformedsearch
Jarrar.lecture notes.aai.2011s.ch3.uniformedsearch
PalGov
 
lecture 17
lecture 17lecture 17
lecture 17
sajinsc
 

Similar to graph coloring.ppt (20)

DAA-Module-5.pptx
DAA-Module-5.pptxDAA-Module-5.pptx
DAA-Module-5.pptx
 
Graph theory
Graph theory Graph theory
Graph theory
 
5.5 back track
5.5 back track5.5 back track
5.5 back track
 
CS 354 Viewing Stuff
CS 354 Viewing StuffCS 354 Viewing Stuff
CS 354 Viewing Stuff
 
problem solve and resolving in ai domain , probloms
problem solve and resolving in ai domain , problomsproblem solve and resolving in ai domain , probloms
problem solve and resolving in ai domain , probloms
 
A* Algorithm
A* AlgorithmA* Algorithm
A* Algorithm
 
Jarrar.lecture notes.aai.2011s.ch3.uniformedsearch
Jarrar.lecture notes.aai.2011s.ch3.uniformedsearchJarrar.lecture notes.aai.2011s.ch3.uniformedsearch
Jarrar.lecture notes.aai.2011s.ch3.uniformedsearch
 
04 numerical
04 numerical04 numerical
04 numerical
 
Comparitive Analysis of Algorithm strategies
Comparitive Analysis of Algorithm strategiesComparitive Analysis of Algorithm strategies
Comparitive Analysis of Algorithm strategies
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview Questions
 
bcfbedbf-6679-4d5d-b8a5-7d4c9c48dba4.pptx
bcfbedbf-6679-4d5d-b8a5-7d4c9c48dba4.pptxbcfbedbf-6679-4d5d-b8a5-7d4c9c48dba4.pptx
bcfbedbf-6679-4d5d-b8a5-7d4c9c48dba4.pptx
 
Lego like spheres and tori, enumeration and drawings
Lego like spheres and tori, enumeration and drawingsLego like spheres and tori, enumeration and drawings
Lego like spheres and tori, enumeration and drawings
 
Optimization problems
Optimization problemsOptimization problems
Optimization problems
 
CS 354 Transformation, Clipping, and Culling
CS 354 Transformation, Clipping, and CullingCS 354 Transformation, Clipping, and Culling
CS 354 Transformation, Clipping, and Culling
 
Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
 
lecture 17
lecture 17lecture 17
lecture 17
 
And or graph problem reduction using predicate logic
And or graph problem reduction using predicate logicAnd or graph problem reduction using predicate logic
And or graph problem reduction using predicate logic
 
24 common mistakes in go (gotchas) and how to avoid them
24 common mistakes in go (gotchas) and how to avoid them24 common mistakes in go (gotchas) and how to avoid them
24 common mistakes in go (gotchas) and how to avoid them
 
Algorithms Design Patterns
Algorithms Design PatternsAlgorithms Design Patterns
Algorithms Design Patterns
 
Ex32018.pdf
Ex32018.pdfEx32018.pdf
Ex32018.pdf
 

More from chetanvchaudhari (8)

chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
 
auditing-190520092523.pdf
auditing-190520092523.pdfauditing-190520092523.pdf
auditing-190520092523.pdf
 
chapter2-190516054412.pdf
chapter2-190516054412.pdfchapter2-190516054412.pdf
chapter2-190516054412.pdf
 
BranchandBoundAlgorithms[1].ppt
BranchandBoundAlgorithms[1].pptBranchandBoundAlgorithms[1].ppt
BranchandBoundAlgorithms[1].ppt
 
chapter16[1].ppt
chapter16[1].pptchapter16[1].ppt
chapter16[1].ppt
 
np complete.ppt
np complete.pptnp complete.ppt
np complete.ppt
 
M.tech computerunitwise
M.tech computerunitwiseM.tech computerunitwise
M.tech computerunitwise
 
Format for address_change_by_mpmla_gazzeted_officer
Format for address_change_by_mpmla_gazzeted_officerFormat for address_change_by_mpmla_gazzeted_officer
Format for address_change_by_mpmla_gazzeted_officer
 

Recently uploaded

Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 

graph coloring.ppt

  • 1. Algorithm Design Techniques Yonglei Tao 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 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
  • 13. AB AD AC DA DC DB EA EC EB BA BD BC ED
  • 14. AB AD AC DA DC DB EA EC EB BA BD BC ED
  • 15. AB AD AC DA DC DB EA EC EB BA BD BC ED
  • 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 }
  • 28. Search Space ... ... ... ... ... ... ...
  • 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
  • 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 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; }