SlideShare a Scribd company logo
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

Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
Kamal Acharya
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdfONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
Kamal Acharya
 

Recently uploaded (20)

Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
 
KIT-601 Lecture Notes-UNIT-4.pdf Frequent Itemsets and Clustering
KIT-601 Lecture Notes-UNIT-4.pdf Frequent Itemsets and ClusteringKIT-601 Lecture Notes-UNIT-4.pdf Frequent Itemsets and Clustering
KIT-601 Lecture Notes-UNIT-4.pdf Frequent Itemsets and Clustering
 
Explosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdfExplosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdf
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
fluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answerfluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answer
 
fundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionfundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projection
 
Toll tax management system project report..pdf
Toll tax management system project report..pdfToll tax management system project report..pdf
Toll tax management system project report..pdf
 
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGBRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
 
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-5 Notes for II-II Mechanical Engineering
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
 
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdfONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
 
Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Kraków
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 

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; }