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

Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
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
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 

Recently uploaded (20)

Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
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
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 

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