0% found this document useful (0 votes)
8 views10 pages

Assignment

The document discusses the greedy algorithm, defining it as a method that makes the best immediate choice without considering future consequences, and provides examples such as the Fractional Knapsack Problem. It also compares arrays and linked lists, highlighting their respective advantages and disadvantages in terms of memory usage, access speed, and efficiency for different scenarios. The conclusion emphasizes the importance of choosing the appropriate data structure based on the specific requirements of the task.

Uploaded by

oksman2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views10 pages

Assignment

The document discusses the greedy algorithm, defining it as a method that makes the best immediate choice without considering future consequences, and provides examples such as the Fractional Knapsack Problem. It also compares arrays and linked lists, highlighting their respective advantages and disadvantages in terms of memory usage, access speed, and efficiency for different scenarios. The conclusion emphasizes the importance of choosing the appropriate data structure based on the specific requirements of the task.

Uploaded by

oksman2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

AIRFORCE INSTITUTE OF TECHNOLOGY KADUNA,

NIGERIA.

COMPUTER SOFTWARE ENGINEERING


(AED 307)

LECTURER: ENG. REX CHARLES

GROUP 3
MEMBERS:
U21AE1014 – SAMUEL IYANUOLUWA OKE
U21AE1015 – AISHA A. KOSOKO
U21AE1017 – DESIRE NLEANYACHUKWU
U21AE1018 – MOHAMMED BELLO SANI
U21AE1019 – AFOLAYAN ISRAEL DIVINE
U2OAE1030 – SOKOMI SETH SARIEL
QUESTION 1: Discuss the concept of greedy algorithm. Provide examples of a problem
where a greedy algorithm is applicable and explain it’s working.

ANSWER TO QUESTION 1

DEFINITION OF TERMS

According to the Oxford English Dictionary, "greedy" means having excessive desire for
something without considering the effect or damage done. While “algorithm” is a set of rules
that must be followed when solving a particular problem.

WHAT IS GREEDY ALGORITHM?

In computer science, a greedy algorithm is an algorithm that finds a solution to problems


in the shortest time possible. It picks the path that ‘seems’ optimal at the moment without
regard for the overall best solution that would be formed.

DR. EDSGER W. DIJKSTRA, a computer scientist and mathematician who wanted to


calculate a minimum spanning tree, introduced the term "Greedy algorithm". Prim and
Kruskal came up with optimization techniques for minimizing cost of graphs.

Many Greedy algorithms were developed to solve graph problems. A graph is a structure
made up of edges and vertices.

Diagram of a simple graph

GREEDY VS NOT GREEDY ALGORITHMS


An algorithm is greedy when the path picked is regarded as the best option based on a
specific criterion without considering future consequences. But it typically evaluates
feasibility before making a final decision. The correctness of the solution depends on the
problem and criteria used.
Example: A graph has various weights and you are to determine the maximum value in the
tree. You'd start by searching each node and checking its weight to see if it is the largest
value.

There are two approaches to solving this problem: greedy approach or not greedy.

Example graph

This graph consists of different weights and we need to find the maximum value. We'll apply
the two approaches on the graph to get the solution.

GREEDY APPROACH
In the images below, a graph has different numbers in its vertices and the algorithm is meant
to select the vertex with the largest number.

Starting from vertex 6, then it's faced with two decisions – which is bigger, 3 or 4? The
algorithm picks 4, and then is faced with another decision – which is bigger, 14 or 11. It
selects 14, and the algorithm ends.

On the other hand, there is a vertex labelled 20 but it is attached to vertex 3 which greedy
does not consider as the best choice. It is important to select appropriate criteria for making
each immediate decision.
The sample graph showing the greedy approach

NOT GREEDY APPROACH


The “not greedy” approach checks all options before arriving at a final solution, unlike the
"greedy approach" which stops once it gets its results.

Starting from vertex 6, then it's faced with two decisions – which is bigger, 3 or 4? The
algorithm picks 4, and then is faced with another decision – which is bigger, 14 or 11. It
selects 14 and keeps it aside.

Then it runs the process again, starting from vertex 6. It selects the vertex with 3 and checks
it. 20 is attached to the vertex 3 and the process stops. Now it compares the two results – 20
and 14. 20 is bigger, so it selects the vertex (3) that carries the largest number and the process
ends.

This approach considers many possibilities in finding the better solution.

The sample graph showing the non-greedy approach

CHARACTERISTICS OF A GREEDY ALGORITHM


1. The algorithm solves its problem by finding an optimal solution. This solution can be a
maximum or minimum value. It makes choices based on the best option available.
2. The algorithm is fast and efficient therefore, applied in solving large-scale problems.
3. The search for optimal solution is done without repetition – the algorithm runs once.
4. It is straightforward and easy to implement.

HOW TO USE GREEDY ALGORITHM


Before applying a greedy algorithm to a problem, you need to ask two questions:

1. Do you need the best option at the moment from the problem?
2. Do you need an optimal solution (either minimum or maximum value)?
If your answer to these questions is "Yes", then a greedy algorithm is a good choice to solve
your problem.

Procedure
Let’s assume you have a problem with a set of numbers and you need to find the minimum
value.

You start of by defining the constraint, which in this case is finding the minimum value. Then
each number will be scanned and checked on each constraint which serves as a condition to
be fulfilled. If the condition is true, the number(s) is selected and returned as the final
solution.

Flow chart showing the process for solving a problem using greedy algorithms

Problem: Fractional Knapsack Problem


A knapsack has a maximum weight, and it can only accommodate a certain set of items.
These items have a weight and a value.

The aim is to fill the knapsack with the items that have the highest total values and do not
exceed the maximum weight capacity.

There are two elements to consider: the knapsack and the items. The knapsack has a
maximum weight and carries some items with a high value.
Scenario: In a jewellery store, there are items made of gold, silver, and wood. The costliest
item is gold followed by silver and then wood. If a jewellery thief comes to the store, they
take gold because they will make the most profit.
The thief has a bag (knapsack) that they can put those items in. But there is a limit to what the
thief can carry because these items can get heavy. The idea is to pick the item the makes the
highest profit and fits in the bag (knapsack) without exceeding its maximum weight.

• The first step is to find the value to weight ratio of all items to know what fraction each
one occupies.

• We then sort these ratios in descending order (from highest to lowest). This way we can
pick the ratios with the highest number first knowing that we will make a profit.

• When we pick the highest ratio, we find the corresponding weight and add it to the
knapsack. Thereby optimising and getting the highest value with as little weight possible

OTHER APPLICATIONS OF GREEDY ALGORITHMS


There are various applications of greedy algorithms. Some of them are:

1. Minimum spanning tree is without any cycles and with the minimum possible total edge
weight. This tree is derived from a connected undirected graph with weights.
2. Dijkstra’s shortest path is a search algorithm that finds the shortest path between a
vertex and other vertices in a weighted graph.
3. Travelling salesman problem involves finding the shortest route that visits different
places only once and returns to the starting point
4. Huffman coding assigns shorter code to frequently occurring symbols and longer code to
less occurring symbols. It is used to encode data efficiently.

ADVANTAGES OF USING A GREEDY ALGORITHM


1. Greedy algorithm is quite straight forward to implement and easy to understand.
2. They are also very efficient and have a lower complexity time.
3. They're useful in solving optimization problems, returning a maximum or minimum
value.

DISADVANTAGES/LIMITATIONS OF USING A GREEDY ALGORITHM


1. Even though greedy algorithms are straightforward and helpful in optimization
problems, they don't offer the best solutions at all times.
2. Also, greedy algorithms only run once, so they don't check the correctness of the
result produced.
CONCLUSION
Greedy algorithm is a type of straight forward problem-solving method that looks for the
best immediate solution without considering future consequences. It tries to make the most
optimal choice (i.e. Most ideal) in the shortest possible way, although this may be often
efficient, it can sometimes lead to sub-optimal solutions.

QUESTION 2: Compare and contrast array and linked list in what scenario will you use one
over the other?
ANSWER TO QUESTION 2

INTRODUCTION

There are many different types of data structures that can be implemented into a computer
program such as arrays and linked lists. Each type of data structure has its strengths and
weaknesses. For these reasons, it is important to understand the advantages and disadvantages
of the different kinds of data structures when it comes to designing, optimizing, and scaling
programs.

ARRAYS

An array is a set of data structure consisting of a collection of elements (values or


variables) of same memory size each identified by one array index or key.
Also, an array is essentially a data structure, similar to a list of values of the same data
type. Another property of arrays is that array elements are stored in continuous memory
locations. Furthermore, arrays have a fixed size which is defined upon initialization.

SCENERIOS WHERE ARRAYS IS USED


1. Storing data of students in a tabular form is best done with array for easy access.

ADVANTAGES OF ARRAYS

Arrays offer several advantages over other data structures, such as:

1. Efficient Access: Arrays provide constant-time access to any element in the collection,
making them ideal for applications that require frequent random access to data.
2. Memory efficiency: Arrays are memory-efficient, as they store data in contiguous memory
locations. This makes them easy to manipulate and efficient to use.

3. Flexibility In Data Type Storage: Arrays can be used to store data of any type, making
them a versatile data structure that can be used in a wide range of applications.

4. Limitless Use: There is no limit to how many times an array can be used.

DISADVANTAGES OF ARRAYS:

Arrays also have some disadvantages, such as:

1. Fixed size: Arrays have a fixed size, which means they cannot be resized once they are
created. This can be problematic in applications where the size of the data set is not known
in advance.

2. Wasted Space: Arrays may waste space if all of the elements in the collection are not
used.

3. Limited Functionality: Arrays have limited functionality compared to other data


structures like linked string

LINKED LIST
Linked list is a fundamental data structure which consists of a collection of nodes where each
node contains a data field and a reference(link) to the next node in the sequence

EXAMPLES OF LINKED LIST


• Single linked list
• Double linked list
• Circular linked list
• Circular doubled linked list

Structure of a linked list

ADVANTAGES OF LINKED LIST


No Memory Wastage: In the linked list, efficient memory utilisation can be achieved since
the size of the linked list increase or decrease. Therefore, there is no memory wastage and
there is no need to pre-allocate the memory.
Implementation: Linear data structure like stacks and queues are often easily implemented
during a linked list.

Insertion And Deletion Operations: Insertion and deletion operation are quite easier in the
linked list. There is no need to shift elements after the insertion or deletion of an element only
the address present in the next pointer needs to be updated.

Scalability: This is the ability to add or remove elements at any position.

Efficient For Large Data Collection: When working with large datasets linked list play a
crucial role as it can grow and shrink dynamically.

DISADVANTAGES OF LINKED LIST


Random Access: Random access is not possible in a liked list due to its dynamic memory
allocation.
Not Suited For Small Data: Can not provide any significant benefit on small datasets
compared to that of array
Lower Efficiency At Times: For certain operations such as searching for an element or
iterating through the list, can be slower in linked list.
Memory Usage: More memory is required in the linked list as compared to an array. Because
in a linked list a pointer is also required to store the address of the next element and it
requires extra memory for itself.
Difficulty In Data Sharing: This is because it is not possible to directly access the memory
address of an element in a linked list

SCENERIO
Scenarios where linked list can is used
1. Image Viewer: In a situation where an image viewer software is to be developed, the
previous and next images can be accessed by the next and previous button.

2. Robotics: Linked list can be used to implement control systems for robots, allowing them
to navigate and interact with their environment.

3. Music Player: Songs in the music player are linked to the previous and next songs,
through linked list algorithm
LINKED LIST
ARRAY

1. Random Access (fast search


time). 1. Fat insertion/deletion time
2. Less memory needed per 2. Dynamic size.
STRENGHT
element. 3. Efficient memory
3. Better cache locality. (i.e. allocation/utilization.
better for easy data retrieval).

1. Slow insertion /deletion time. 1. Slow search time.


WEAKNESS 2. Fixed size. 2. More memory is needed as
3. Inefficient memory. additional storage required for
pointers.

CONCLUSION

There are many different data structures. Each data structure has strengths and
weaknesses which affect performance depending on the task. Arrays allow random access
and require less memory per element (i.e. do not need space for pointers) while lacking
efficiency for insertion/deletion operations and memory allocation.
On the contrary, linked lists are dynamic and have faster insertion/deletion time
complexities. However, linked list have a slower search time and pointers require additional
memory per element in the list.

You might also like