You are on page 1of 26

Project 1

Rhythm Game in Java Programming Language

2SE1 Group 6:
Muhammad Fajrul Falah
Muhammad Yusuf Ilham Abdulhadi
Rivan Anjung Sinar Permana

Faculty:
Mr. Muhammad Riza Iqbal

Continuing Education Center for Computing and Information Technology


Faculty of Engineering, University of Indonesia
2023
PREFACE

First of all, definitely we would like to thank God the Almighty, who has given his
blessing to the author for finishing these papers.

In its preparation, these papers consist of four chapters; consisting of Chapter I as the
Introduction of these papers, Chapter II will be discussing the Basic Theory, Chapter III
Problem Analysis, Chapter IV Conclusion and Suggestion of these papers.

As authors, we acknowledge that our limited knowledge and experience may have
resulted in certain limitations in this paper. Consequently, we humbly request constructive
criticism and suggestions from our readers to help us enhance and refine this work. We hope
that these papers can be a reference for choosing topics Dijkstra's Algorithm to find the
optimal route.

Depok, March 2023

Author

i
TABLE OF CONTENTS

PREFACE............................................................................................................i
TABLE OF CONTENTS...................................................................................ii

TABLE OF FIGURES......................................................................................iv

TABLE OF TABLES.........................................................................................v

CHAPTER I: INTRODUCTION......................................................................1

I.1. Background................................................................................................1

I.2. Writing Objective.......................................................................................2

I.3. Problem Domain.........................................................................................2

I.4. Writing Methodology.................................................................................2

I.5. Writing Framework....................................................................................3

CHAPTER II: BASIC THEORY......................................................................4

II.1. The Definition of Algorithm.....................................................................4

II.2. A Brief History of Algorithm....................................................................4

II.3. The Importance of Algorithm...................................................................6

CHAPTER III: PROBLEM ANALYSIS.........................................................8

III.1. Definition of Dijkstra’s Algorithm..........................................................8

III.2. How Dijkstra’s Algorithm Works...........................................................8

III.3. Example of Dijkstra’s Algorithm............................................................9

III.4. Code implementation of Dijkstra’s Algorithm in Java..........................12

III.5. Comparison Dijkstra with A* Algorithm..............................................14

III.6. Applications of Dijkstra’s shortest path algorithm................................14

CHAPTER IV: CONCLUSION AND SUGGESTION.................................16

IV.1. Conclusion.............................................................................................16
ii
IV.2. Suggestion.............................................................................................16

BIBLIOGRAPHY.............................................................................................17

APPENDIX........................................................................................................18

iii
TABLE OF FIGURES

Figure 1.1 Weighted Graphs .......................................................................9


Figure 1.2 Starting node and assigned value to the rest ...........................9
Figure 1.3 Determining the shortest path from the starting node and
replacing the infinity value...........................................................................9
Figure 1.4 Move to the next shortest path node as the reference node. .10
Figure 1.5 Repeat Step 4.............................................................................10
Figure 1.6 Junction......................................................................................11
Figure 1.7 Final Vertex ..............................................................................11
Figure 1.8 All the vertices has been visited...............................................11

iv
TABLE OF TABLES

Table 1.1 Comparison of Dijkstra with A* Algorithm........................14

v
CHAPTER I: INTRODUCTION

I.1. Background
Today, the entire world is digitalised. Every typical device that makes our lives so
easy and fast has an intellect and a feeling of communication. These technological
advancements are aided by software, a collection of programmes designed to solve a specific
problem. And every programme is based on an Algorithm, which is a logic/solution.

People often ask us, “what is an algorithm?” To answer them, an algorithm is a step-
by-step technique that specifies a series of instructions that must be followed in a precise
order to achieve the intended outcome. Algorithms are usually designed independently of the
underlying programming languages, which implies that they may be implemented in several
languages. An algorithm's properties include ambiguity, fineness, efficacy, and language
independence. An algorithm's scalability and performance are the major elements that
influence its relevance.

An algorithm is a collection of instructions that a computer must follow to execute


computations or other problem-solving tasks. An algorithm is a finite set of instructions
carried out in a certain order to execute a job according to its formal specification.

It is not the whole programme or code; rather, it is basic reasoning for a problem
expressed in the form of a flowchart or pseudocode.

● Problem: A issue is a real-world problem or a real-world example problem for which


you must create a programme or set of instructions. A set of instructions is referred to
as an algorithm.
● Algorithm: An algorithm is a step-by-step technique that is created to solve a
problem.
● Input: The algorithm is given the required and desirable inputs after being designed.
● Processing Unit: The data will be sent to the processing unit, which will provide the
desired result.
● Output: The output of a programme is the outcome or result of the programme.

1
Dijkstra's Algorithm is a popular problem-solving technique used in computer science
and mathematics to find the shortest path between two points in a graph. The main purpose of
using Dijkstra's algorithm is to find the optimal route between two points on a graph

2
2

which is essential in many real-life applications such as navigation systems, logistics


planning, and network optimization.

The Dijkstra’s Algorithm guarantees to find the shortest path between two nodes in a
weighted graph, where the edges have a weight or distance assigned to them. Dijkstra's
algorithm is a powerful tool for solving optimization problems that involve finding the
shortest path between two points, and it has been widely adopted in many fields to improve
efficiency and reduce costs.

I.2. Writing Objective


To explain the concept and application of Dijkstra's algorithm for finding the shortest
path between two points on a graph. The writing should aim to provide a clear understanding
of the algorithm's definition, its technique, and code implementation.

I.3. Problem Domain


The issues for the domain are as follow:

a. Definition of Algorithm, History of Algorithm, and Function of Algorithm


b. Definition of Dijkstra’s Algorithm
c. The technique of using Dijkstra’s Algorithm to find the optimal route.
d. Code implementation of Dijkstra’s Algorithm in Java Programming Language
e. Comparison Dijkstra with A* Algorithm
f. Applications of Dijkstra’s shortest path algorithm

I.4. Writing Methodology


The methodology used in writing these papers is descriptive technique, using the data
from a reliable source of literature.
3

I.5. Writing Framework


Chapter 1 Introduction:
In chapter I, we will discuss the background, writing objectives, problem domain, writing
methodology, and writing framework as the introduction of these papers.
Chapter 2 Basic Theory:
In chapter II, we will discuss the basic theory of these papers consisting of: Definition
of Algorithm, History of Algorithm, and Function of Algorithm.

Chapter 3 Problem Analysis:


In chapter III, we will analyze the problem of this topic consisting of: Definition of
Dijkstra’s Algorithm, the technique of using Dijkstra’s Algorithm to find the optimal route,
code implementation of Dijkstra’s Algorithm in Java Programming Language, comparison
Dijkstra with A* Algorithm and applications of Dijkstra’s shortest path algorithm.

Chapter 4 Conclusion and Suggestion:


In this chapter, we will review the conclusions and recommendations of these papers,
based on the knowledge gained from our research from various sources.
CHAPTER II: BASIC THEORY

II.1. The Definition of Algorithm


An algorithm is a step-by-step procedure that defines a set of instructions that must be
carried out in a specific order to produce the desired result. In computer science, an algorithm
is a procedure used for solving a problem or performing a computation. Algorithms act as an
exact list of instructions that conduct specified actions step by step in either hardware- or
software-based routines. Algorithms are generally developed independently of underlying
languages, which means that an algorithm can be implemented in more than one
programming language.

Algorithms are widely used throughout all areas of IT. In mathematics and computer
science, an algorithm usually refers to a small procedure that solves a recurrent problem.
Algorithms are also used as specifications for performing data processing and play a major
role in automated systems.

An algorithm could be used for sorting sets of numbers or for more complicated tasks,
like recommending user content on social media. Algorithms typically start with initial input
and instructions that describe a specific computation. When the computation is executed, the
process produces an output.

Unambiguity, fineness, effectiveness, and language independence are some of the


characteristics of an algorithm. The scalability and performance of an algorithm are the
primary factors that contribute to its importance.

II.2. A Brief History of Algorithm


“In this digital age, algorithms are ubiquitous! From your phone to your computer to
even your microwave, practically every device you use has something to do with algorithms.
They are the literal foundation of every technology known to man. Given their importance in
our daily lives, it’s worth taking a brief look at them to better understand what they are and
how they became so relevant in our lives.”

The term algorithm got its name from the Persian astronomer and mathematician,
Abu Abdullah Muhammad ibn Musa Al-Khawarizmi (780 AD), dubbed the father of algebra

4
5

(al-jabr). He was from a Persian city known as Khwarizm, found in present-day


Uzbekistan. The Persian Arabs (or Arabs in general) were given very common names like
Muhammad, Musa, Abdullah, and so they would (and still) differentiate each other by their
location (city, state, etc). In this context, if John Doe were American, he’d be known as John
Doe al-Amriki, i.e., John Doe the American (to set him apart from John Doe the Canadian).
Similarly, Muhammad ibn (son of) Musa became known as “al-Khwarizmi”, meaning the
native of Khwarizm (in Arabic the letter ‘ya’, from which stems the ‘i/ee’ sound, is added to
the end of a word to denote ‘belonging to’). Al-Khwarizmi wrote many important
mathematical books that were later translated into Latin. It was his book on Arabic-Hindu
numerals and arithmetic, called Al-Khwārizmī On the Hindu Art of Reckoning, later Latinized
as Algoritmi de numero Indorum that gave the word ‘algorithm’ to the Western world.

An algorithm is a series of step-by-step instructions that aim to solve a particular


problem. From that definition, they have existed for as long as humans have been striving to
solve problems on earth. Thus, things like recipes, mathematics, and physics are all forms of
algorithms. It can be argued that the creation of the first artificial fire in the Wonderwerk
Cave of South Africa, millions of years ago by Homo Erectus, constitutes our first evidence of
the use of algorithms. However, most historians hold the view that the Babylonian clay
tablets (1600–1800 BC) are the world’s first known algorithm. The Babylonians had
developed a numerical system using cuneiform numerals to count and they preserved those
calculations on tablets.

Although we can find instances of algorithms in Euclidean mathematics, Archimedes’


approximation of Pi, or Eratosthenes’ calculation of prime numbers, it was the work of
George Boole in developing what is known as Boolean algebra, in 1847, that set the basis for
computer logic today. Boole developed a system of logic to make calculations that used true
and false values as basic units (binary algebra). This system would later be represented in
digital form by 0’s and 1’s or binary digits in low-level programming languages.

Some decades later, Alan Turing came up with a mathematical model of a


hypothetical computing machine. It used squares that contained symbols or binary digits in an
6

infinitely long tape. This tape served as storage space for the data in the squares, computer
memory.
7

It has a ‘head’ or needle that can move to the right or left of each square to read,
write, or erase the symbol within it, the basis for CRUD (Create, Read, Update, and Delete)
operations in programming. Using this system this machine could simulate any algorithm,
regardless of its complexity!

Mathematicians and computer scientists continued adding to Turing’s concept of a


computing machine to advance technology to what we know today. Algorithms then evolved
from binary operations to high-level, more human-friendly, programming languages like C+
+, Java, and JavaScript. Because they allow creating efficient, error-free software
applications, they will continue to be important in the foreseeable future.

II.3. The Importance of Algorithm


Algorithms are employed across the field of computer science. They are the backbone
of the field. In computer science, an algorithm is a collection of instructions that allows a
computer to perform any task, such as operating a calculator or launching a rocket.

At their foundation, computer programmes are algorithms defined in programming


languages that the machine understands. The way computer algorithms heavily influence
social media works: which postings appear, which adverts are visible, and so on. Algorithms
make all of these judgments.

Google engineers use algorithms to improve searches, forecast what users will enter,
etc. Knowing how to create an algorithm is an important component of computer
programming when it comes to problem-solving.

There are two crucial aspects of the algorithm:

● Theoretical significance: You must break down a real-world challenge into smaller
modules when provided one. To deconstruct the issue, you must first grasp all of its
theoretical components.
8

● Practical Significance: As you are all aware, the theory is useless until it is put into
practice. As a consequence, the theoretical and practical relevance of algorithms may
be evaluated.
CHAPTER III: PROBLEM ANALYSIS

III.1. Definition of Dijkstra’s Algorithm


Dijkstra's Algorithm is a popular problem-solving technique used in computer science
and mathematics to find the shortest path between two points in a graph. The main purpose of
using Dijkstra's algorithm is to find the optimal route between two points on a graph, which is
essential in many real-life applications such as navigation systems, logistics planning, and
network optimization.

The dijkstra’s algorithm guarantees to find the shortest path between two nodes in a
weighted graph, where the edges have a weight or distance assigned to them. Dijkstra's
algorithm is a powerful algorithm for solving optimization problems that involve finding the
shortest path between two points, and it has been widely adopted in many fields to improve
efficiency and reduce costs.

Dijkstra’s algorithm also requires the graphs to have positive weights. If there’s a
negative weight in the graph, it will change and mess with the fundamental step in which the
Dijkstra Algorithm works.

III.2. How Dijkstra’s Algorithm Works


The fundamental step of Dijkstra's algorithm starts with determining a node as a
starting node. Then the algorithm also analyzes the graph to find the shortest path or route
between the starting node and every other node. The algorithm also records the current
shortest path that is known currently from the starting node for each node. It will also update
its records if the algorithm finds a much shorter path.

The algorithm then marks the just calculated node as "visited" or has been calculated
into the algorithm records. This step is to prevent the algorithm from calculating that node
again. This process continues until all the nodes in the graph are in the algorithm records.
With this, we have complete records for all of the shortest paths from the starting node to
each node.

8
9

III.3. Example of Dijkstra’s Algorithm

Figure 1.1 Weighted Graphs (REF : Programiz)


Step 1 : Start with weighted graphs. we will refer to the vertex as a node and the edges as a
path.

Figure 1.2 Starting node and assigned value to the rest (REF : Programiz)
Step 2 : Determine a starting node in the graphs and assign infinity value to the rest of the
unvisited node. and marks the starting node as “visited” node
10

Figure 1.3 Determining the shortest path from the starting node and replacing the
infinity value (REF : Programiz)
Step 3 : Go to each vertex that is connected with the starting node and determine the shortest
path between the two from the starting node and replace the infinity with the shortest path for
each of the nodes. Also update its path length.

Figure 1.4 Move to the next shortest path node as the reference node (REF : Programiz)
Step 4 : After determining the shortest path, we shall move the references node to the next
shortest path. We will put the next shortest that we will visit as a “visited” node. Then we will
determine the next shortest path from the current references node. If the path length of the
next shortest path from the reference node is lesser than the new path length, then don’t
update it.

Figure 1.5 Repeat Step 4 (REF : Programiz)


Step 5 : Repeat step 4 for each node, and avoid updating path lengths of already visited node.
11

Figure 1.6 Junction (REF : Programiz)


Step 6 : if we find a junction between 2 nodes we will choose the shortest path or the lowest
value node. so we choose 5 before 7.

Figure 1.7 Final Vertex (REF : Programiz)


Step 7 : Notice how the rightmost vertex has its path length updated twice.

Figure 1.8 All the vertices has been visited (REF : Programiz)
Step 8 : Repeat until the rest of the vertices is visited
12

III.4. Code implementation of Dijkstra’s Algorithm in Java


We found an example of Dijkstra's algorithm implementation in java from
“Programiz”. This website provides a clear a concise implementation of the algorithm. The
code is written in a modular and organized manner, making it easy to understand and modify
for different use cases. This example code can be used as a starting point for anyone looking
to implement Dijkstra's algorithm.

public static void dijkstra(int[][] graph, int source) {

int count = graph.length;

boolean[] visitedVertex = new boolean[count];

int[] distance = new int[count];

for (int i = 0; i < count; i++) {

visitedVertex[i] = false;

distance[i] = Integer.MAX_VALUE;

// Distance of self loop is zero

distance[source] = 0;

for (int i = 0; i < count; i++) {

// Update the distance between neighbouring vertex and source vertex

int u = findMinDistance(distance, visitedVertex);

visitedVertex[u] = true;

// Update all the neighbouring vertex distances

for (int v = 0; v < count; v++) {

if (!visitedVertex[v] && graph[u][v] != 0 && (distance[u] +


graph[u][v] < distance[v])) {

distance[v] = distance[u] + graph[u][v];

for (int i = 0; i < distance.length; i++) {


13

System.out.println(String.format("Distance from %s to %s is %s",


source, i, distance[i]));

// Finding the minimum distance

private static int findMinDistance(int[] distance, boolean[]


visitedVertex) {

int minDistance = Integer.MAX_VALUE;

int minDistanceVertex = -1;

for (int i = 0; i < distance.length; i++) {

if (!visitedVertex[i] && distance[i] < minDistance) {

minDistance = distance[i];

minDistanceVertex = i;

return minDistanceVertex;

public static void main(String[] args) {

int graph[][] = new int[][] { { 0, 0, 1, 2, 0, 0, 0 }, { 0, 0, 2, 0, 0,


3, 0 }, { 1, 2, 0, 1, 3, 0, 0 },

{ 2, 0, 1, 0, 0, 0, 1 }, { 0, 0, 3, 0, 0, 2, 0 }, { 0, 3, 0, 0, 2,
0, 1 }, { 0, 0, 0, 1, 0, 1, 0 } };

Dijkstra T = new Dijkstra();

T.dijkstra(graph, 0);

}
14

III.5. Comparison Dijkstra with A* Algorithm

Parameter Dijkstra A*
Search Algorithm Best First Search Greedy Best First Search

Exploration Strategy Explores all possible paths Prioritizes nodes with the
lowest distance from the
starting node and the lowest
estimated distance to the
goal

Efficiency Slower for larger graphs Faster for larger graphs

Application Suitable for small to medium- Suitable for medium to


sized graphs large-sized graphs
Table 1.1 Comparison of Dijkstra with A* Algorithm (REF: Baeldung, Kevin Wang)
Two of the most popular techniques for determining the shortest path between two
points in a graph are Dijkstra's algorithm and the A* algorithm. Both algorithms are capable
of solving this issue, however their methods and efficiency differ in terms of exploration
strategy and temporal complexity. We determined the difference of them from various
source, Kevin Wang on his Youtube channel explain the difference between them in a visual
way.

Based from our analysis, it shows that Dijkstra is way slower when facing many
obstacle and larger maps, while A* faster in larger map even with many obstacles.

III.6. Applications of Dijkstra’s shortest path algorithm


A popular approach for determining the shortest route between two nodes in a graph
is called Dijkstra's Algorithm. It has several practical uses in various sectors. From Geeks for
Geeks, there are various of application to use, such as digital mapping programs like Google
Maps are one of its most well-known uses. The strategy is used in these services to determine
the shortest path between any two points. The path between two cities/places is represented
as an edge, and each city or place is represented as a node. The shortest paths between any
two cities or locations, or from one city or place to another, may be determined by applying
Dijkstra's Algorithm to this graph.

Telephone networks also utilize the algorithm. Each line in a telephone network has a
bandwidth, and the transmission line's bandwidth is the maximum frequency it can handle.
15

The bandwidth is a representation of how much data may be delivered across the connection.
If we visualize a city as a graph, the switching stations would be the vertices, and the
transmission lines would be the edges. The length of the line is represented by the weight of
the edges. We can determine the shortest distance between two switching stations using
Dijkstra's Algorithm.
CHAPTER IV: CONCLUSION AND SUGGESTION

IV.1. Conclusion
The Dijkstra algorithm is still an effective tool for handling optimization issues.
Dijkstra's method is a reliable option for a variety of real-life applications, such as navigation
systems, network optimization, etc. Since it ensures that the best path will be found in a
weighted graph with positive weights.

In contrast to the A* method, the approach might be slower for bigger networks and
requirements that the graphs have positive weights. Despite these limitations, Dijkstra's
Algorithm is often used and has been shown to be successful in resolving optimization issues
involving shortest distance between two points.

IV.2. Suggestion
We hope that this paper will be useful, especially when applying Dijkstra's method to
daily issues of locating the best routes or pathfinding (in this case, finding the closest one).
To increase productivity and cut expenses, it is advised to use this algorithm in a variety of
fields such as network optimization and navigation systems.

16
BIBLIOGRAPHY

dipesh99kumar. (2022, 9 23). Applications of Dijkstra’s shortest path algorithm. Retrieved


from Geeksforgeeks: https://www.geeksforgeeks.org/applications-of-dijkstras-
shortest-path-algorithm/ [14/03/2023]

Gillis, A. S. (2022, 5). Algorithm. Retrieved from TechTarget:


https://www.techtarget.com/whatis/definition/algorithm [14/03/2023]

Goyal, A., Mogha, P., Luthra, R., & Sangwan, N. (2014). PATH FINDING: A* OR
DIJKSTRA’S? . IJITE.

JetLearn. (2023, 1 26). What Is An Algorithm? Its Uses And Importance In Coding. Retrieved
from JetLearn: https://www.jetlearn.com/blog/algorithm-importance-in-coding
[14/03/2023]

Prabhu, R. (2023, 3 15). What is Algorithm | Introduction to Algorithms. Retrieved from


Geeksforgeeks: https://www.geeksforgeeks.org/introduction-to-algorithms/
[14/03/2023]

Programiz. (n.d.). Dijkstra's Algorithm. Retrieved from Programiz:


https://www.programiz.com/dsa/dijkstra-algorithm [14/03/2023]

Pyram, J. (2020, 7 18). A Brief History of Algorithms and their Impact on the World.
Retrieved from Medium: https://levelup.gitconnected.com/a-brief-look-at-algorithms-
and-their-impact-on-the-world-2794f8dd70aa [14/03/2023]

Simic, M. (22, 11 8). Dijkstra vs. A* – Pathfinding. Retrieved from Baeldung:


https://www.baeldung.com/cs/dijkstra-vs-a-pathfinding [14/03/2023]

Upadhyay, S. (2023, 2 21). What Is An Algorithm? Characteristics, Types and How to write
it. Retrieved from Simplilearn: https://www.simplilearn.com/tutorials/data-structure-
tutorial/what-is-an-algorithm [14/03/2023]

Wang, K. (2015). Compare A* with Dijkstra algorithm. Retrieved from Youtube:


https://www.youtube.com/watch?v=g024lzsknDo [14/03/2023]

17
APPENDIX

18

You might also like