SlideShare a Scribd company logo
Unit-3
CHARACTERISTICS OF GREEDY ALGORITHM, ACTIVITY SELECTION
PROBLEM,ELEMENTS OF GREEDY STRATEGY, MST
Greedy Algorithm
 In greedy algorithm approach, decisions are made from the given solution
domain. As being greedy, the closest solution that seems to provide an
optimum solution is chosen.
 Greedy algorithms try to find a localized optimum solution, which may
eventually lead to globally optimized solutions. However, generally greedy
algorithms do not provide globally optimized solutions.
Explanation of Greedy algorithm by
counting coin problem
If we are provided coins of ₹ 1, 2, 5 and 10 and we are asked to count ₹ 18
then the greedy procedure will be −
 1 − Select one ₹ 10 coin, the remaining count is 8
 2 − Then select one ₹ 5 coin, the remaining count is 3
 3 − Then select one ₹ 2 coin, the remaining count is 1
 4 − And finally, the selection of one ₹ 1 coins solves the problem
❖ Greedy algorithm always try to pick the largest possible coin.
But if we slightly change the problem then the same approach may not be
able to produce the same optimum result.
 For the currency system, where we have coins of 1, 7, 10 value, counting
coins for value 18 will be absolutely optimum but for count like 15, it may
use more coins than necessary. For example, the greedy approach will use
10 + 1 + 1 + 1 + 1 + 1, total 6 coins. Whereas the same problem could be
solved by using only 3 coins (7 + 7 + 1)
 Hence, we may conclude that the greedy approach picks an immediate
optimized solution and may fail where global optimization is a major
concern.
Examples
Most networking algorithms use the greedy approach. Here is a list of few of them
−
 Travelling Salesman Problem
 Prim's Minimal Spanning Tree Algorithm
 Kruskal's Minimal Spanning Tree Algorithm
 Dijkstra's Minimal Spanning Tree Algorithm
 Graph - Map Coloring
 Graph - Vertex Cover
 Knapsack Problem
 Job Scheduling Problem
Activity Selection Problem
 Let us consider the Activity Selection problem as our first example of
Greedy algorithms. Following is the problem statement.
You are given n activities with their start and finish times. Select the
maximum number of activities that can be performed by a single person,
assuming that a person can only work on a single activity at a time.
The greedy choice is to always pick the next activity whose finish time is least among the remaining activities
and the start time is more than or equal to the finish time of the previously selected activity. We can sort the
activities according to their finishing time so that we always consider the next activity as minimum finishing
time activity.
1) Sort the activities according to their finishing time
2) Select the first activity from the sorted array and print it.
3) Do the following for the remaining activities in the sorted array.
…….a) If the start time of this activity is greater than or equal to the finish time of the previously selected
activity then select this activity and print it.
In the following C implementation, it is assumed that the activities are already sorted according to their finish
time.
What is a Spanning Tree?
 Given an undirected and connected graph G=(V,E), a spanning tree of
the graph G is a tree that spans G (that is, it includes every vertex of G)
and is a subgraph of G (every edge in the tree belongs to G)
What is a Minimum Spanning Tree?
 The cost of the spanning tree is the sum of the weights of all the edges in
the tree. There can be many spanning trees. Minimum spanning tree is the
spanning tree where the cost is minimum among all the spanning trees.
There also can be many minimum spanning trees.
Example of MST
Kruskal’s Algorithm
 Kruskal’s Algorithm builds the spanning tree by adding edges one by one
into a growing spanning tree. Kruskal's algorithm follows greedy approach
as in each iteration it finds an edge which has least weight and add it to
the growing spanning tree.
Algorithm Steps:
 Sort the graph edges with respect to their weights.
 Start adding edges to the MST from the edge with the smallest weight until
the edge of the largest weight.
 Only add edges which doesn't form a cycle , edges which connect only
disconnected components.
How does Kruskal's algorithm work?
In Kruskal's algorithm, we start from edges with the lowest weight and keep
adding the edges until the goal is reached. The steps to implement Kruskal's
algorithm are listed as follows -
 First, sort all the edges from low weight to high.
 Now, take the edge with the lowest weight and add it to the spanning tree.
If the edge to be added creates a cycle, then reject the edge.
 Continue to add the edges until we reach all vertices, and a minimum
spanning tree is created.
The applications of Kruskal's algorithm are -
 Kruskal's algorithm can be used to layout electrical wiring among cities.
 It can be used to lay down LAN connections.
Example of Kruskal's algorithm
Now, let's see the working of Kruskal's algorithm using an example. It will be easier to understand Kruskal's algorithm using an
example.
Suppose a weighted graph is -
The weight of the edges of the above graph is given in the below table -
Edge AB AC AD AE BC CD DE
Weight 1 7 10 5 3 4 2
In summary, Kruskal's algorithm requires-
•A worst-case time complexity of O(E(log(E)).
•An average-case time complexity of O(E(log(E)).
•A best-case time complexity of O(E(log(E)).
•A space complexity of O(E+V).
Analysis-
•The edges are maintained as min heap.
•The next edge can be obtained in O(logE) time if graph has E edges.
•Reconstruction of heap takes O(E) time.
•So, Kruskal’s Algorithm takes O(ElogE) time.
•The value of E can be at most O(V2).
•So, O(logV) and O(logE) are same.
PRACTICE PROBLEMS BASED ON KRUSKAL’S
ALGORITHM-
Prims Algorithm
 Prim's Algorithm is a greedy algorithm that is used to find the minimum
spanning tree from a graph. Prim's algorithm finds the subset of edges that
includes every vertex of the graph such that the sum of the weights of the
edges can be minimized.
 Prim's algorithm starts with the single node and explores all the adjacent
nodes with all the connecting edges at every step. The edges with the
minimal weights causing no cycles in the graph got selected.
How does the prim's algorithm work?
 Prim's algorithm is a greedy algorithm that starts from one vertex and
continue to add the edges with the smallest weight until the goal is
reached. The steps to implement the prim's algorithm are given as follows -
 First, we have to initialize an MST with the randomly chosen vertex.
 Now, we have to find all the edges that connect the tree in the above
step with the new vertices. From the edges found, select the minimum
edge and add it to the tree.
 Repeat step 2 until the minimum spanning tree is formed.
Example of prim's algorithm
Now, let's see the working of prim's algorithm using an example. It will be easier to understand the prim's
algorithm using an example.
Suppose, a weighted graph is -
Algorithm
1.Step 1: Select a starting vertex
2.Step 2: Repeat Steps 3 and 4 until there are fringe vertices
3.Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that has minimum
weight
4.Step 4: Add the selected edge and the vertex to the minimum spanning tree T
5.[END OF LOOP]
6.Step 5: EXIT
Difference between Prim’s Algorithm and Kruskal’s Algorithm-
Prim’s Algorithm Kruskal’s Algorithm
The tree that we are making or growing always
remains connected.
The tree that we are making or growing usually
remains disconnected.
Prim’s Algorithm grows a solution from a random
vertex by adding the next cheapest vertex to the
existing tree.
Kruskal’s Algorithm grows a solution from the
cheapest edge by adding the next cheapest edge
to the existing tree / forest.
Prim’s Algorithm is faster for dense graphs. Kruskal’s Algorithm is faster for sparse graphs.
Calculate MST using Prims
Solution MST will be
Solve for MST

More Related Content

Similar to Unit3_1.pdf

Grady approach prim's spanning tree
Grady approach prim's spanning tree Grady approach prim's spanning tree
Grady approach prim's spanning tree
Anwar Ullah
 
MST
MSTMST
Shortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfShortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdf
zefergaming
 
Module 2 - Greedy Algorithm Data structures and algorithm
Module 2  - Greedy Algorithm  Data structures and algorithmModule 2  - Greedy Algorithm  Data structures and algorithm
Module 2 - Greedy Algorithm Data structures and algorithm
farzanirani201402
 
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
SintooChauhan6
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
Muhammad Amjad Rana
 
Bfs dfs mst
Bfs dfs mstBfs dfs mst
Bfs dfs mst
AvichalVishnoi
 
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhCh3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
danielgetachew0922
 
DA lecture 3.pptx
DA lecture 3.pptxDA lecture 3.pptx
DA lecture 3.pptx
SayanSen36
 
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy Techniques
Nirmalavenkatachalam
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer science
Riazul Islam
 
Spanning Tree in data structure and .pptx
Spanning Tree in data structure and .pptxSpanning Tree in data structure and .pptx
Spanning Tree in data structure and .pptx
asimshahzad8611
 
Data Analysis and Algorithms Lecture 1: Introduction
 Data Analysis and Algorithms Lecture 1: Introduction Data Analysis and Algorithms Lecture 1: Introduction
Data Analysis and Algorithms Lecture 1: Introduction
TayyabSattar5
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
Tekle12
 
Algorithms Lab PPT
Algorithms Lab PPTAlgorithms Lab PPT
Algorithms Lab PPT
Abhishek Chandra
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms  notesAnalysis and Design of Algorithms  notes
Analysis and Design of Algorithms notes
Prof. Dr. K. Adisesha
 
A greedy algorithms
A greedy algorithmsA greedy algorithms
A greedy algorithms
Amit Kumar Rathi
 
Approximation Algorithms TSP
Approximation Algorithms   TSPApproximation Algorithms   TSP
DAA 1 ppt.pptx
DAA 1 ppt.pptxDAA 1 ppt.pptx
DAA 1 ppt.pptx
RAJESH S
 

Similar to Unit3_1.pdf (20)

Grady approach prim's spanning tree
Grady approach prim's spanning tree Grady approach prim's spanning tree
Grady approach prim's spanning tree
 
MST
MSTMST
MST
 
Shortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfShortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdf
 
Module 2 - Greedy Algorithm Data structures and algorithm
Module 2  - Greedy Algorithm  Data structures and algorithmModule 2  - Greedy Algorithm  Data structures and algorithm
Module 2 - Greedy Algorithm Data structures and algorithm
 
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
Decision Maths 1 Chapter 3 Algorithms on Graphs (including Floyd A2 content)....
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
Bfs dfs mst
Bfs dfs mstBfs dfs mst
Bfs dfs mst
 
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhCh3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
 
DA lecture 3.pptx
DA lecture 3.pptxDA lecture 3.pptx
DA lecture 3.pptx
 
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy Techniques
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer science
 
Spanning Tree in data structure and .pptx
Spanning Tree in data structure and .pptxSpanning Tree in data structure and .pptx
Spanning Tree in data structure and .pptx
 
Data Analysis and Algorithms Lecture 1: Introduction
 Data Analysis and Algorithms Lecture 1: Introduction Data Analysis and Algorithms Lecture 1: Introduction
Data Analysis and Algorithms Lecture 1: Introduction
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
 
Algorithms Lab PPT
Algorithms Lab PPTAlgorithms Lab PPT
Algorithms Lab PPT
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms  notesAnalysis and Design of Algorithms  notes
Analysis and Design of Algorithms notes
 
36 greedy
36 greedy36 greedy
36 greedy
 
A greedy algorithms
A greedy algorithmsA greedy algorithms
A greedy algorithms
 
Approximation Algorithms TSP
Approximation Algorithms   TSPApproximation Algorithms   TSP
Approximation Algorithms TSP
 
DAA 1 ppt.pptx
DAA 1 ppt.pptxDAA 1 ppt.pptx
DAA 1 ppt.pptx
 

Recently uploaded

Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
Peter Caitens
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Why React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdfWhy React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdf
ayushiqss
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
Jelle | Nordend
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
XfilesPro
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
NaapbooksPrivateLimi
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 

Recently uploaded (20)

Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Why React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdfWhy React Native as a Strategic Advantage for Startup Innovation.pdf
Why React Native as a Strategic Advantage for Startup Innovation.pdf
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 

Unit3_1.pdf

  • 1. Unit-3 CHARACTERISTICS OF GREEDY ALGORITHM, ACTIVITY SELECTION PROBLEM,ELEMENTS OF GREEDY STRATEGY, MST
  • 2. Greedy Algorithm  In greedy algorithm approach, decisions are made from the given solution domain. As being greedy, the closest solution that seems to provide an optimum solution is chosen.  Greedy algorithms try to find a localized optimum solution, which may eventually lead to globally optimized solutions. However, generally greedy algorithms do not provide globally optimized solutions.
  • 3. Explanation of Greedy algorithm by counting coin problem If we are provided coins of ₹ 1, 2, 5 and 10 and we are asked to count ₹ 18 then the greedy procedure will be −  1 − Select one ₹ 10 coin, the remaining count is 8  2 − Then select one ₹ 5 coin, the remaining count is 3  3 − Then select one ₹ 2 coin, the remaining count is 1  4 − And finally, the selection of one ₹ 1 coins solves the problem ❖ Greedy algorithm always try to pick the largest possible coin.
  • 4. But if we slightly change the problem then the same approach may not be able to produce the same optimum result.  For the currency system, where we have coins of 1, 7, 10 value, counting coins for value 18 will be absolutely optimum but for count like 15, it may use more coins than necessary. For example, the greedy approach will use 10 + 1 + 1 + 1 + 1 + 1, total 6 coins. Whereas the same problem could be solved by using only 3 coins (7 + 7 + 1)  Hence, we may conclude that the greedy approach picks an immediate optimized solution and may fail where global optimization is a major concern.
  • 5. Examples Most networking algorithms use the greedy approach. Here is a list of few of them −  Travelling Salesman Problem  Prim's Minimal Spanning Tree Algorithm  Kruskal's Minimal Spanning Tree Algorithm  Dijkstra's Minimal Spanning Tree Algorithm  Graph - Map Coloring  Graph - Vertex Cover  Knapsack Problem  Job Scheduling Problem
  • 6. Activity Selection Problem  Let us consider the Activity Selection problem as our first example of Greedy algorithms. Following is the problem statement. You are given n activities with their start and finish times. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time.
  • 7. The greedy choice is to always pick the next activity whose finish time is least among the remaining activities and the start time is more than or equal to the finish time of the previously selected activity. We can sort the activities according to their finishing time so that we always consider the next activity as minimum finishing time activity. 1) Sort the activities according to their finishing time 2) Select the first activity from the sorted array and print it. 3) Do the following for the remaining activities in the sorted array. …….a) If the start time of this activity is greater than or equal to the finish time of the previously selected activity then select this activity and print it. In the following C implementation, it is assumed that the activities are already sorted according to their finish time.
  • 8. What is a Spanning Tree?  Given an undirected and connected graph G=(V,E), a spanning tree of the graph G is a tree that spans G (that is, it includes every vertex of G) and is a subgraph of G (every edge in the tree belongs to G) What is a Minimum Spanning Tree?  The cost of the spanning tree is the sum of the weights of all the edges in the tree. There can be many spanning trees. Minimum spanning tree is the spanning tree where the cost is minimum among all the spanning trees. There also can be many minimum spanning trees.
  • 10. Kruskal’s Algorithm  Kruskal’s Algorithm builds the spanning tree by adding edges one by one into a growing spanning tree. Kruskal's algorithm follows greedy approach as in each iteration it finds an edge which has least weight and add it to the growing spanning tree. Algorithm Steps:  Sort the graph edges with respect to their weights.  Start adding edges to the MST from the edge with the smallest weight until the edge of the largest weight.  Only add edges which doesn't form a cycle , edges which connect only disconnected components.
  • 11.
  • 12. How does Kruskal's algorithm work? In Kruskal's algorithm, we start from edges with the lowest weight and keep adding the edges until the goal is reached. The steps to implement Kruskal's algorithm are listed as follows -  First, sort all the edges from low weight to high.  Now, take the edge with the lowest weight and add it to the spanning tree. If the edge to be added creates a cycle, then reject the edge.  Continue to add the edges until we reach all vertices, and a minimum spanning tree is created. The applications of Kruskal's algorithm are -  Kruskal's algorithm can be used to layout electrical wiring among cities.  It can be used to lay down LAN connections.
  • 13. Example of Kruskal's algorithm Now, let's see the working of Kruskal's algorithm using an example. It will be easier to understand Kruskal's algorithm using an example. Suppose a weighted graph is - The weight of the edges of the above graph is given in the below table - Edge AB AC AD AE BC CD DE Weight 1 7 10 5 3 4 2
  • 14. In summary, Kruskal's algorithm requires- •A worst-case time complexity of O(E(log(E)). •An average-case time complexity of O(E(log(E)). •A best-case time complexity of O(E(log(E)). •A space complexity of O(E+V). Analysis- •The edges are maintained as min heap. •The next edge can be obtained in O(logE) time if graph has E edges. •Reconstruction of heap takes O(E) time. •So, Kruskal’s Algorithm takes O(ElogE) time. •The value of E can be at most O(V2). •So, O(logV) and O(logE) are same.
  • 15. PRACTICE PROBLEMS BASED ON KRUSKAL’S ALGORITHM-
  • 16.
  • 17. Prims Algorithm  Prim's Algorithm is a greedy algorithm that is used to find the minimum spanning tree from a graph. Prim's algorithm finds the subset of edges that includes every vertex of the graph such that the sum of the weights of the edges can be minimized.  Prim's algorithm starts with the single node and explores all the adjacent nodes with all the connecting edges at every step. The edges with the minimal weights causing no cycles in the graph got selected.
  • 18. How does the prim's algorithm work?  Prim's algorithm is a greedy algorithm that starts from one vertex and continue to add the edges with the smallest weight until the goal is reached. The steps to implement the prim's algorithm are given as follows -  First, we have to initialize an MST with the randomly chosen vertex.  Now, we have to find all the edges that connect the tree in the above step with the new vertices. From the edges found, select the minimum edge and add it to the tree.  Repeat step 2 until the minimum spanning tree is formed.
  • 19. Example of prim's algorithm Now, let's see the working of prim's algorithm using an example. It will be easier to understand the prim's algorithm using an example. Suppose, a weighted graph is -
  • 20.
  • 21. Algorithm 1.Step 1: Select a starting vertex 2.Step 2: Repeat Steps 3 and 4 until there are fringe vertices 3.Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that has minimum weight 4.Step 4: Add the selected edge and the vertex to the minimum spanning tree T 5.[END OF LOOP] 6.Step 5: EXIT
  • 22. Difference between Prim’s Algorithm and Kruskal’s Algorithm- Prim’s Algorithm Kruskal’s Algorithm The tree that we are making or growing always remains connected. The tree that we are making or growing usually remains disconnected. Prim’s Algorithm grows a solution from a random vertex by adding the next cheapest vertex to the existing tree. Kruskal’s Algorithm grows a solution from the cheapest edge by adding the next cheapest edge to the existing tree / forest. Prim’s Algorithm is faster for dense graphs. Kruskal’s Algorithm is faster for sparse graphs.