SlideShare a Scribd company logo
1 of 13
CHAPTER 7 Greedy Algorithms
Algorithm 7.1.1 Greedy Coin Changing This algorithm makes change for an amount  A  using coins of denominations denom [1] >  denom [2] > ··· >  denom [ n ] = 1. Input Parameters:  denom , A Output Parameters: None greedy_coin_change ( denom , A ) { i  = 1 while ( A  > 0) { c  =  A / denom [ i ] println (“use ” +  c  + “ coins of denomination ” +  denom [ i ]) A  =  A  -  c  *  denom [ i ] i  =  i  + 1 } }
Algorithm 7.2.4 Kruskal’s Algorithm Kruskal’s algorithm finds a minimal spanning tree in a connected, weighted graph with vertex set {1, ... ,  n } . The input to the algorithm is  edgelist , an array of  edge , and  n . The members of edge are •  v  and  w , the vertices on which the edge is incident. •  weight , the weight of the edge. The output lists the edges in a minimal spanning tree. The function  sort  sorts the array  edgelist  in nondecreasing order of weight.
Input Parameters:  edgelist , n Output Parameters: None kruskal ( edgelist , n ) { sort ( edgelist ) for  i  = 1 to  n makeset ( i ) count  = 0 i  = 1 while ( count  <  n  - 1) { if ( findset ( edgelist [ i ]. v ) !=  findset ( edgelist [ i ]. w )) { println ( edgelist [ i ]. v  + “ ”  +  edgelist [ i ]. w ) count  =  count  + 1 union ( edgelist [ i ]. v , edgelist [ i ]. w ) } i  =  i  + 1 } }
Algorithm 7.3.4 Prim’s Algorithm This algorithm finds a minimal spanning tree in a connected, weighted,  n -vertex graph. The graph is represented using adjacency lists;  adj [ i ] is a reference to the first node in a linked list of nodes representing the vertices adjacent to vertex  i . Each node has members  ver , the vertex adjacent to  i ;  weight , representing the weight of edge ( i , ver ); and  next , a reference to the next node in the linked list or null, for the last node in the linked list. The start vertex is  start . In the minimal spanning tree, the parent of vertex  i   ≠   start  is  parent [ i ], and  parent [ start ] = 0. The value  ∞  is the largest available integer value.
Input Parameters:  adj , start Output Parameters:  parent prim ( adj , start , parent ) { n  =  adj . last for  i  = 1 to  n key [ i ] = ∞  //  key  is a local array key [ start ] = 0 parent [ start ] = 0 // the following statement initializes the // container  h  to the values in the array  key h . init ( key , n ) for  i  = 1 to  n  { v  =  h . del () ref  =  adj [ v ] while ( ref  != null) { w  =  ref . ver if ( h . isin ( w ) &&  ref . weight  <  h . keyval ( w )) { parent [ w ] =  v h . decrease ( w , ref . weight ) } ref  =  ref . next } } }
Algorithm 7.4.4 Dijkstra’s Algorithm This algorithm finds shortest paths from the designated vertex  start  to all of the other vertices in a connected, weighted,  n -vertex graph. The graph is represented using adjacency lists;  adj [ i ] is a reference to the first node in a linked list of nodes representing the vertices adjacent to vertex  i . Each node has members  ver , the vertex adjacent to  i ;  weight , representing the weight of edge ( i , ver ); and  next , a reference to the next node in the linked list or null, for the last node in the linked list. In a shortest path, the predecessor of vertex  i   start is  predecessor [ i ], and  predecessor [ star t]  = 0. The value  ∞  is the largest available integer value. The abstract data type  h  supports the same operations as in Prim’s algorithm.
Input Parameters:  adj , start Output Parameters:  parent dijkstra ( adj , start , parent ) { n  =  adj . last for  i  = 1 to  n key [ i ] = ∞  //  key  is a local array key [ start ] = 0 predecessor [ start ] = 0 ...
... // the following statement initializes the // container  h  to the values in the array  key h . init ( key , n ) for  i  = 1 to  n  { v  =  h . min_weight_index () min_cost  =  h . keyval ( v ) v  =  h . del () ref  =  adj [ v ] while ( ref  != null) { w  =  ref . ver if ( h . isin ( w ) &&  min_cost  +  ref . weight  <  h . keyval ( w )) { predecessor [ w ] =  v h . decrease ( w ,  min_cost + ref . weight ) } // end if ref  =  ref . next } // end while } // end for }
Algorithm 7.5.3 Huffman’s Algorithm This algorithm constructs an optimal Huffman coding tree. The input is an array  a  of  n  = 2 nodes. Each node has an integer member  character  to identify a particular character, another integer member  key  to identify that character’s frequency, and  left  and  right  members. After the Huffman coding tree is constructed, a left member of a node references its left child, and a right member of a node references its right child or, if the node is a terminal vertex, its left and right members are null. The algorithm returns a reference to the root of the Huffman coding tree. The operator, new, is used to obtain a new node. If  a  is an array, the expression h . init ( a ) initializes the container  h  to the data in  a . The expression h . del () deletes the node in  h  with the smallest key and returns the node. The expression h . insert ( ref  ) inserts the node referenced by  ref  into  h .
Input Parameters:  a Output Parameters: None huffman ( a ) { h . init ( a ) for  i  = 1 to  a . last  - 1 { ref  = new  node ref . left  =  h . del () ref . right  =  h . del () ref . key  =  ref . left . key  +  ref . right . key h . insert ( ref ) } return  h . del () }
Algorithm 7.6.2 Greedy Algorithm for the Continuous-Knapsack Problem The input to the algorithm is the knapsack capacity  C , and an array  a  of size  n , each of whose entries specifies an  id  (e.g., the first item might have  id  1, the second item might have  id  2, etc.), a profit  p , and a weight  w . The output tells how much of each object to select to maximize the profit. Objects not selected do not appear in the output. The function  sort  sorts the array  a  in nonincreasing order of the ratio of profit to weight.
Input Parameters:  a , C Output Parameters: None continuous_knapsack ( a , C )  { n  =  a . last for  i  = 1 to  n ratio [ i ] =  a [ i ]. p / a [ i ]. w sort ( a , ratio ) weight  = 0 i  = 1 while ( i  ≤  n  &&  weight  <  C )  { if ( weight  +  a [ i ]. w  =  C ) { println (“select all of object ” +  a [ i ]. id ) weight  =  weight  +  a [ i ]. w } else { r  = ( C  -  weight )/ a [ i ]. w println(“select ” + r + “ of object ” +  a [ i ]. id ) weight  =  C } i  =  i  + 1 } }

More Related Content

What's hot (19)

Lec1
Lec1Lec1
Lec1
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
 
Numpy string functions
Numpy string functionsNumpy string functions
Numpy string functions
 
Strinng Classes in c++
Strinng Classes in c++Strinng Classes in c++
Strinng Classes in c++
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in java
 
Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Introducing Assignment invalidates the Substitution Model of Evaluation and v...Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Introducing Assignment invalidates the Substitution Model of Evaluation and v...
 
Unitii string
Unitii stringUnitii string
Unitii string
 
Greedy method by Dr. B. J. Mohite
Greedy method by Dr. B. J. MohiteGreedy method by Dr. B. J. Mohite
Greedy method by Dr. B. J. Mohite
 
Lec30
Lec30Lec30
Lec30
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Lec22
Lec22Lec22
Lec22
 
Strings
StringsStrings
Strings
 
Function
Function Function
Function
 
Recursive squaring
Recursive squaringRecursive squaring
Recursive squaring
 
C# Strings
C# StringsC# Strings
C# Strings
 
MATLAB for Technical Computing
MATLAB for Technical ComputingMATLAB for Technical Computing
MATLAB for Technical Computing
 
Mechanical Engineering Homework Help
Mechanical Engineering Homework HelpMechanical Engineering Homework Help
Mechanical Engineering Homework Help
 

Viewers also liked

Quantitative methods minimal spanning tree and dijkstra
Quantitative methods minimal spanning tree and dijkstraQuantitative methods minimal spanning tree and dijkstra
Quantitative methods minimal spanning tree and dijkstraadarsh
 
Tecnicas investig livros-internet...
Tecnicas investig livros-internet...Tecnicas investig livros-internet...
Tecnicas investig livros-internet...arturramisio
 
Nove formaty v online marketingu
Nove formaty v online marketinguNove formaty v online marketingu
Nove formaty v online marketinguDaniel Zaicek
 

Viewers also liked (7)

antivirus
antivirusantivirus
antivirus
 
Quantitative methods minimal spanning tree and dijkstra
Quantitative methods minimal spanning tree and dijkstraQuantitative methods minimal spanning tree and dijkstra
Quantitative methods minimal spanning tree and dijkstra
 
Apple
AppleApple
Apple
 
Tecnicas investig livros-internet...
Tecnicas investig livros-internet...Tecnicas investig livros-internet...
Tecnicas investig livros-internet...
 
Nove formaty v online marketingu
Nove formaty v online marketinguNove formaty v online marketingu
Nove formaty v online marketingu
 
Steve jobs
Steve jobsSteve jobs
Steve jobs
 
18 August Statistical Analysis
18 August Statistical Analysis18 August Statistical Analysis
18 August Statistical Analysis
 

Similar to Chap07alg

Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to ArraysTareq Hasan
 
Write a program that reads a connected graph from a file and displays.docx
 Write a program that reads a connected graph from a file and displays.docx Write a program that reads a connected graph from a file and displays.docx
Write a program that reads a connected graph from a file and displays.docxajoy21
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyappasami
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programmingPrudhviVuda
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Deepak John
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arraysmaamir farooq
 
Chp-1 Quick Review of basic concepts.pdf
Chp-1 Quick Review of basic concepts.pdfChp-1 Quick Review of basic concepts.pdf
Chp-1 Quick Review of basic concepts.pdfSolomonMolla4
 
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdfIn C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdfstopgolook
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfaroraopticals15
 
ARRAY in python and c with examples .pptx
ARRAY  in python and c with examples .pptxARRAY  in python and c with examples .pptx
ARRAY in python and c with examples .pptxabhishekmaurya102515
 
Below is the assignment description and the file I have written..pdf
Below is the assignment description and the file I have written..pdfBelow is the assignment description and the file I have written..pdf
Below is the assignment description and the file I have written..pdfinfo673628
 

Similar to Chap07alg (20)

Chap12alg
Chap12algChap12alg
Chap12alg
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Write a program that reads a connected graph from a file and displays.docx
 Write a program that reads a connected graph from a file and displays.docx Write a program that reads a connected graph from a file and displays.docx
Write a program that reads a connected graph from a file and displays.docx
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
 
Chap04alg
Chap04algChap04alg
Chap04alg
 
Chap04alg
Chap04algChap04alg
Chap04alg
 
Ada notes
Ada notesAda notes
Ada notes
 
Array
ArrayArray
Array
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
Chp-1 Quick Review of basic concepts.pdf
Chp-1 Quick Review of basic concepts.pdfChp-1 Quick Review of basic concepts.pdf
Chp-1 Quick Review of basic concepts.pdf
 
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdfIn C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
ARRAY in python and c with examples .pptx
ARRAY  in python and c with examples .pptxARRAY  in python and c with examples .pptx
ARRAY in python and c with examples .pptx
 
Chap08alg
Chap08algChap08alg
Chap08alg
 
Chap08alg
Chap08algChap08alg
Chap08alg
 
Below is the assignment description and the file I have written..pdf
Below is the assignment description and the file I have written..pdfBelow is the assignment description and the file I have written..pdf
Below is the assignment description and the file I have written..pdf
 

More from Munkhchimeg (20)

Protsesor
ProtsesorProtsesor
Protsesor
 
Lecture916
Lecture916Lecture916
Lecture916
 
Lecture915
Lecture915Lecture915
Lecture915
 
Lecture914
Lecture914Lecture914
Lecture914
 
Lecture913
Lecture913Lecture913
Lecture913
 
Lecture911
Lecture911Lecture911
Lecture911
 
Lecture912
Lecture912Lecture912
Lecture912
 
Lecture910
Lecture910Lecture910
Lecture910
 
Lecture5
Lecture5Lecture5
Lecture5
 
Lecture9
Lecture9Lecture9
Lecture9
 
Lecture8
Lecture8Lecture8
Lecture8
 
Lecture7
Lecture7Lecture7
Lecture7
 
Lecture6
Lecture6Lecture6
Lecture6
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture3
Lecture3Lecture3
Lecture3
 
Ded Algorithm
Ded AlgorithmDed Algorithm
Ded Algorithm
 
Ded Algorithm1
Ded Algorithm1Ded Algorithm1
Ded Algorithm1
 
Tobch Lecture
Tobch LectureTobch Lecture
Tobch Lecture
 
Lecture914
Lecture914Lecture914
Lecture914
 
Tobch Lecture
Tobch LectureTobch Lecture
Tobch Lecture
 

Recently uploaded

Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 

Recently uploaded (20)

Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 

Chap07alg

  • 1. CHAPTER 7 Greedy Algorithms
  • 2. Algorithm 7.1.1 Greedy Coin Changing This algorithm makes change for an amount A using coins of denominations denom [1] > denom [2] > ··· > denom [ n ] = 1. Input Parameters: denom , A Output Parameters: None greedy_coin_change ( denom , A ) { i = 1 while ( A > 0) { c = A / denom [ i ] println (“use ” + c + “ coins of denomination ” + denom [ i ]) A = A - c * denom [ i ] i = i + 1 } }
  • 3. Algorithm 7.2.4 Kruskal’s Algorithm Kruskal’s algorithm finds a minimal spanning tree in a connected, weighted graph with vertex set {1, ... , n } . The input to the algorithm is edgelist , an array of edge , and n . The members of edge are • v and w , the vertices on which the edge is incident. • weight , the weight of the edge. The output lists the edges in a minimal spanning tree. The function sort sorts the array edgelist in nondecreasing order of weight.
  • 4. Input Parameters: edgelist , n Output Parameters: None kruskal ( edgelist , n ) { sort ( edgelist ) for i = 1 to n makeset ( i ) count = 0 i = 1 while ( count < n - 1) { if ( findset ( edgelist [ i ]. v ) != findset ( edgelist [ i ]. w )) { println ( edgelist [ i ]. v + “ ” + edgelist [ i ]. w ) count = count + 1 union ( edgelist [ i ]. v , edgelist [ i ]. w ) } i = i + 1 } }
  • 5. Algorithm 7.3.4 Prim’s Algorithm This algorithm finds a minimal spanning tree in a connected, weighted, n -vertex graph. The graph is represented using adjacency lists; adj [ i ] is a reference to the first node in a linked list of nodes representing the vertices adjacent to vertex i . Each node has members ver , the vertex adjacent to i ; weight , representing the weight of edge ( i , ver ); and next , a reference to the next node in the linked list or null, for the last node in the linked list. The start vertex is start . In the minimal spanning tree, the parent of vertex i ≠ start is parent [ i ], and parent [ start ] = 0. The value ∞ is the largest available integer value.
  • 6. Input Parameters: adj , start Output Parameters: parent prim ( adj , start , parent ) { n = adj . last for i = 1 to n key [ i ] = ∞ // key is a local array key [ start ] = 0 parent [ start ] = 0 // the following statement initializes the // container h to the values in the array key h . init ( key , n ) for i = 1 to n { v = h . del () ref = adj [ v ] while ( ref != null) { w = ref . ver if ( h . isin ( w ) && ref . weight < h . keyval ( w )) { parent [ w ] = v h . decrease ( w , ref . weight ) } ref = ref . next } } }
  • 7. Algorithm 7.4.4 Dijkstra’s Algorithm This algorithm finds shortest paths from the designated vertex start to all of the other vertices in a connected, weighted, n -vertex graph. The graph is represented using adjacency lists; adj [ i ] is a reference to the first node in a linked list of nodes representing the vertices adjacent to vertex i . Each node has members ver , the vertex adjacent to i ; weight , representing the weight of edge ( i , ver ); and next , a reference to the next node in the linked list or null, for the last node in the linked list. In a shortest path, the predecessor of vertex i start is predecessor [ i ], and predecessor [ star t] = 0. The value ∞ is the largest available integer value. The abstract data type h supports the same operations as in Prim’s algorithm.
  • 8. Input Parameters: adj , start Output Parameters: parent dijkstra ( adj , start , parent ) { n = adj . last for i = 1 to n key [ i ] = ∞ // key is a local array key [ start ] = 0 predecessor [ start ] = 0 ...
  • 9. ... // the following statement initializes the // container h to the values in the array key h . init ( key , n ) for i = 1 to n { v = h . min_weight_index () min_cost = h . keyval ( v ) v = h . del () ref = adj [ v ] while ( ref != null) { w = ref . ver if ( h . isin ( w ) && min_cost + ref . weight < h . keyval ( w )) { predecessor [ w ] = v h . decrease ( w , min_cost + ref . weight ) } // end if ref = ref . next } // end while } // end for }
  • 10. Algorithm 7.5.3 Huffman’s Algorithm This algorithm constructs an optimal Huffman coding tree. The input is an array a of n = 2 nodes. Each node has an integer member character to identify a particular character, another integer member key to identify that character’s frequency, and left and right members. After the Huffman coding tree is constructed, a left member of a node references its left child, and a right member of a node references its right child or, if the node is a terminal vertex, its left and right members are null. The algorithm returns a reference to the root of the Huffman coding tree. The operator, new, is used to obtain a new node. If a is an array, the expression h . init ( a ) initializes the container h to the data in a . The expression h . del () deletes the node in h with the smallest key and returns the node. The expression h . insert ( ref ) inserts the node referenced by ref into h .
  • 11. Input Parameters: a Output Parameters: None huffman ( a ) { h . init ( a ) for i = 1 to a . last - 1 { ref = new node ref . left = h . del () ref . right = h . del () ref . key = ref . left . key + ref . right . key h . insert ( ref ) } return h . del () }
  • 12. Algorithm 7.6.2 Greedy Algorithm for the Continuous-Knapsack Problem The input to the algorithm is the knapsack capacity C , and an array a of size n , each of whose entries specifies an id (e.g., the first item might have id 1, the second item might have id 2, etc.), a profit p , and a weight w . The output tells how much of each object to select to maximize the profit. Objects not selected do not appear in the output. The function sort sorts the array a in nonincreasing order of the ratio of profit to weight.
  • 13. Input Parameters: a , C Output Parameters: None continuous_knapsack ( a , C ) { n = a . last for i = 1 to n ratio [ i ] = a [ i ]. p / a [ i ]. w sort ( a , ratio ) weight = 0 i = 1 while ( i ≤ n && weight < C ) { if ( weight + a [ i ]. w = C ) { println (“select all of object ” + a [ i ]. id ) weight = weight + a [ i ]. w } else { r = ( C - weight )/ a [ i ]. w println(“select ” + r + “ of object ” + a [ i ]. id ) weight = C } i = i + 1 } }