greedy algorithm Flashcards
1
Q
Greedy algorithm
A
- It is used to solve optimization problems optimization problem is that demand either maximum or minimum results
- Greedy method is simplest and straightforward approach
- The main function of this approach is taken on the basis of currently available information whatever the information is present the decision is made without worrying about effect of current decision in the future
- This technique is basically used to determine the feasible solution that may be optimal or not the feasible solution is a subset that satisfies the given criteria
2
Q
Characteristics of greedy method
A
- This algorithm creates two sets where one set contains all schmuzen items and the other set contains all the rejected items
2.
3
Q
Components of greedy algorithm
A
- canditate set: Solution that is created from the set
- Selection function: this function is used to choose the candidate or subset which can be added in the solution
- Feasibility function: a function that is used to determine whether the candidate or subset can be used to contribute to the solution or not
- Objective function : A function is used to assign the value to the solution or the partial solution
- Solution function: this function is used to intimate Whether the complete function has been reached or not
4
Q
Applications of greedy method
A
- Finding the shortest path
- Finding the minimum spanning tree using trim’s algorithm or Krushkal’s algorithm
- Job sequencing with the deadline
- fractional knapsack problem
5
Q
Disadvantages of greedy Algorithm
A
- Greedy algorithm makes decision based on the information available at each phase without considering the future problem so this might be possible that greedy solution does not give best solution for every problem
- It follows local optimum choice at each stage with intend Or finding global optimum
6
Q
fractional knapsack
A
- in fractional sap the items are broken in order to maximise the profit
- Ingredients approach we calculate the ratio of profit/weight And accordingly we select
- the item with the highest ratio we selected first
7
Q
Three approaches to solve Fractional knapsack problem
A
- Select item based on max profit
- Select item based on min weight
- Calculate the ratio of profit/weight
- Would be best one Among all the approaches
- Hereafter calculating ratio each item is added into knapsack in Descending order of their wild value
8
Q
spanning tree
A
- A tree is represented G(V, E) Where B is vertices and er edges
- And the spaling tree of the graph is represented as G
(V
,E)
- Here in GRAPH and spanning tree vertices are equal but Edges will be 1 less than vertices
(E= |V|-1)
9
Q
Working
A
- First we need to draw all the possible spanning trees for a graph
- Then calculate the cost
- Choose the spanning tree which costs less— It is called as minimum spanning tree
10
Q
Conditions Existing in minimum spanning tree
A
- Number of vertices will be same V
=V
- Member of edges in minimum spanning tree will be one less than number of vertices in spanning tree——– (E
= |V|-1)
- Spanning tree should not contain any cycle
- tree should not be disconnected
11
Q
Krushkal’s algorithm for mst
A
- It is an algorithm to construct minimum spanning tree for connected weighted graphs
- Time complexity is (E log E) or (E log V)
- First arrange edges of a graph in order of increasing weight in a table
- It should contain v=v
- It should not form any loop or cycle
- Now create a minimum spanning tree With the edges According to the ascending order minimum to the maximum without creating a loop or a cycle
- Finally the MST created by Kruskal’s algorithm should contain equal number of vertices and one less number of edges
12
Q
prim’s M S T
A
- Another method of mst
- Here also Vertices should be equal and edges should be one less than the number of vertices
- Here we need Create two sets one that contains the vertices which are included already in mst and the ones which Did not include
- Then we need to choose the starting point or node
- From the starting node we need to cheque all the Connected or linked notes and Select the minimum cost one
- Now we need to add the vertices which are selected into one set and which are not selected into the other set because it is very important to also remember the notes which are not selected because in the further steps we need to compare all the nodes which has minimum cost
- From the next node we need to Find all the linked notes and compare between the older possible notes and the newer possible notes and select the minimum cost one
- Make sure that you’re not creating a loop or a cycle
- And after traversing all the vertices you create another graph which is called as panning tree
- It contains equal number of vertices and one less edge compared to vertex
13
Q
Difference between Kruskal’s algorithm and prim’s Algorithm
A
in Kruskal’s algorithm while creating mst we get Unlinked edges But finally connects without Making loop whereas in Primm algorithm by creating only mst will be linked
14
Q
dijkstra algo
Single source shortest path
A
- Used in Google Maps dna mapping social network these all are represented in graphs
- Here we need to find shortest path from a single source to multiple paths
- time Complexity is o(v^2)
- This algorithm begins at a node which is the source node and examines the graph to find the shortest path between That node and all the other nodes in the graph
- This algorithm keeps the record of all the visited notes and it updates if it finds shorter path
- Once the algorithm has retrieved the shortest path the node is marked as visited and included in the path
- The procedure continues until all the notes in the graph have been included in the path
- In this manner we have a path connecting the source node to all other notes following the shortest path possible to reach each
- Each vertex in algorithm have two properties
- Visited property
- Path property
- Works only on weighted graphs And with directed and undirected graphs
15
Q
Visited and path property
A
- Visited property signifies whether the node is visited or not this property is used to avoid revisiting of the node
- The path property stores the value of current minimum path To the node. The property is revised when any neighbour of the node is visited This property is significant because it stores the final answer for each node