Docsity

ASM1 - Data Structures and Algorithms, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
ASSIGNMENT 1 FRONT SHEET
Qualification BTEC Level 5 HND Diploma in Computing
Unit number and title Unit 19: Data Structures and Algorithms
Submission date 18/08/2021
Date Received 1st
submission
Re-submission Date Date Received 2nd
submission
Student Name Vo Thanh Nhan Student ID GCD191366
Class GCD0806 Assessor name Ho Van Phi
Student declaration
I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that
making a false declaration is a form of malpractice.
Student’s signature Vo Thanh Nhan
Grading grid
P1 P2 P3 M1 M2 M3 D1 D2
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
Discount

On special offer

Partial preview of the text

Download ASM1 - Data Structures and Algorithms and more Assignments Data Structures and Algorithms in PDF only on Docsity!

ASSIGNMENT 1 FRONT SHEET

Qualification BTEC Level 5 HND Diploma in Computing Unit number and title Unit 19: Data Structures and Algorithms Submission date 18/08/2021 Date Received 1st submission Re-submission Date Date Received 2nd submission Student Name Vo Thanh Nhan Student ID GCD Class GCD0806 Assessor name Ho Van Phi Student declaration I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that making a false declaration is a form of malpractice.

Student’s signature Vo Thanh Nhan

Grading grid P1 P2 P3 M1 M2 M3 D1 D

 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date: Internal Verifier’s Comments: IV Signature:

TABLE OF CONTENTS

  • 1 Chapter 1: INTRODUCTION..................................................................................................................
    • 1.1 SCENARIO....................................................................................................................................................
    • 1.2 REPORT OBJECTIVES.....................................................................................................................................
    • 1.3 REPORT OUTLINE.........................................................................................................................................
  • 2 Chapter 2: DATA STRUCTURES............................................................................................................
    • 2.1 ABSTRACT DATA TYPE (P1)...........................................................................................................................
      • 2.1.1 Definition........................................................................................................................................................................
      • 2.1.2 Example of ADT..............................................................................................................................................................
    • 2.2 ADT USAGES...............................................................................................................................................
      • 2.2.1 Application of Stack in memory (P2)............................................................................................................................
      • 2.2.2 Application of an ADT (P3)...........................................................................................................................................
      • 2.2.3 Application of Queue (M1)...........................................................................................................................................
    • 2.3 ADT AND OOP (M3, D2).............................................................................................................................
      • 2.3.1 What is OOP.................................................................................................................................................................
      • 2.3.2 Using OOP to represent ADT........................................................................................................................................
      • 2.3.3 Discussion.....................................................................................................................................................................
  • 3 Chapter 3: ALGORITHMS...................................................................................................................
    • 3.1 SORTING ALGORITHMS (M2)......................................................................................................................
      • 3.1.1 Selection sort................................................................................................................................................................
      • 3.1.2 Quick sort......................................................................................................................................................................
      • 3.1.3 Comparing between Selection sort and Quick sort.....................................................................................................
    • 3.2 SHORTEST PATH ALGORITHMS (D1)...........................................................................................................
      • 3.2.1 Definition of graph structure........................................................................................................................................
      • 3.2.2 Terminology for graph..................................................................................................................................................
      • 3.2.3 Ways to representation of a graph.............................................................................................................................
      • 3.2.4 Dijkstra’s algorithm......................................................................................................................................................
      • 3.2.5 Bellman Ford’s algorithm.............................................................................................................................................
  • 4 Chapter 4: BIBLIOGRAPHY.................................................................................................................
  • 5 Chapter 5: INDEX OF COMMENT.......................................................................................................
  • Figure 1: Example of table.............................................................................................................................. TABLE OF FIGURES
  • Figure 2: Result of the code about arraylist.................................................................................................
  • Figure 3: Last In First Out (LIFO)...................................................................................................................
  • Figure 4: Pushing and popping in stack........................................................................................................
  • Figure 5: Frame pointer (EBP) and stack pointer (ESP)................................................................................
  • Figure 6: The return address and saved frame pointer................................................................................
  • Figure 7: Choose for viewing the student list...............................................................................................
  • Figure 8: Choose for viewing students having age bigger than 20...............................................................
  • Figure 9: Choose for finding a specific student using ID...............................................................................
  • Figure 10: Choose for adding new student...................................................................................................
  • Figure 11: The list after adding new student................................................................................................
  • Figure 12: The list after removing a student................................................................................................
  • Figure 13: The student list after being sorted..............................................................................................
  • Figure 14: The enqueue and dequeue operation.........................................................................................
  • Figure 15: Single-lane one-way road............................................................................................................
  • Figure 16: Ticket counter..............................................................................................................................
  • Figure 17: Insert new patients to the list......................................................................................................
  • Figure 18: Find a specific patient in the list..................................................................................................
  • Figure 19: Find the next person and remove...............................................................................................
  • Figure 20: Working with linked list...............................................................................................................
  • Figure 21: Select the first element as minimum...........................................................................................
  • Figure 22: Compare minimum with the remaining elements......................................................................
  • Figure 23: Swap the first with minimum......................................................................................................
  • Figure 24: The first iteration.........................................................................................................................
  • Figure 25: The second iteration....................................................................................................................
  • Figure 26: The third iteration........................................................................................................................
  • Figure 27: The fourth iteration.....................................................................................................................
  • Figure 28: Time complexity of Selection sort...............................................................................................
  • Figure 29: Space complexity of Selection sort..............................................................................................
  • Figure 30: Working with Selection sort........................................................................................................
  • Figure 31: Select a pivot element.................................................................................................................
  • Figure 32: Pull all the smaller and greater elements....................................................................................
  • Figure 33: Comparison of pivot element with the first index element........................................................
  • Figure 34: Set a second pointer....................................................................................................................
  • Figure 35: Pivot is compared with other elements......................................................................................
  • Figure 36: Repeat the process to set the next greater element as the second pointer..............................
  • Figure 37: The process goes on until reaching the second last element.....................................................
  • Figure 38: Swap the pivot element with the second pointer.......................................................................
  • Figure 39: Select the pivot element of in each half and put at correct place..............................................
  • Figure 40: Time complexity of Quick sort.....................................................................................................
  • Figure 41: Space complexity of Quick sort...................................................................................................
  • Figure 42: Working with Quick sort..............................................................................................................
  • Figure 43: Compare performance between Quick sort and Selection sort..................................................
  • Figure 44: Example of graph structure.........................................................................................................
  • Figure 45: Graph adjacency matrix...............................................................................................................
  • Figure 46: Adjacency list representation......................................................................................................
  • Figure 47: Each sub path is the shortest path..............................................................................................
  • Figure 48: Step 1 of Dijkstra’s algorithm......................................................................................................
  • Figure 49: Step 2 of Dijkstra’s algorithm......................................................................................................
  • Figure 50: Step 3 of Dijkstra’s algorithm......................................................................................................
  • Figure 51: Step 4 of Dijkstra’s algorithm......................................................................................................
  • Figure 52: Step 5 of Dijkstra’s algorithm......................................................................................................

1.2 REPORT OBJECTIVES

This report was created for the purpose of accomplishing the following 3 main contents: First of all, it explains on how to specify an abstract data type using the example of software stack. Second, it includes a detailed explanation of the advantages of encapsulation and information hiding when using an ADT. Finally, it also discusses of imperative ADTs with regard to object orientation.

1.3 REPORT OUTLINE

The structure of this report is organized as follows: Chapter 1 introduces a scenario and points out the objective of this report. Chapter 2 is about data structures. To be specific, the definition about abstract data type (ADT) and common ways for representing it are included. Moreover, it also creates a design specification for data structures with explaining the valid operation which can be carried out on the structures. Not only that, it also discusses about ADT usages. Firstly, it determines the operations of a memory stack and how it is used for implementing function calls in a computer. Moreover, after giving an imperative definition, specifying the ADT for a software stack, this chapter then illustrates a concrete data structure for a FIFO queue. Finally, chapter 2 points out the view about the opinion that imperative ADTs are a basis for object orientation. Chapter 3 takes responsibility for comparing the performance of two sorting algorithms are selection sort and quick sort. Last but not least, in this chapter, it also has the part of analyzing the operation using illustrations of two network shortest path algorithms. Chapter 4 contains citations from the sources used by this report. Chapter 5 is the space for the index of comments.

2 Chapter 2: DATA STRUCTURES

2.1 ABSTRACT DATA TYPE (P1)

2.1.1 Definition

2.1.1.1 Abstract data type (ADT) The abstract data type is fundamentally a type of data which can be utilized in several computer program. It means the type like integer, float, etc; the space such as numbers will take 4-bytes, character will take 1-byte of space, and so on. The abstract datatype is special kind of datatype, whose behavior is characterized by a set of values and set of operations. The keyword “abstract” is utilized as these datatypes can be used and different operations could be performed. Nevertheless, how those operations are working that is completely covered up from the client. The ADT is made of with primitive datatypes, but operation rationales are hidden. When it comes to floating point numbers, for instance, people do not have to understand how floating-point arithmetic works in order to utilize float. Indeed, depending on the processor, even a virtual coprocessor, the details can differ. Some numeric ADTs are built-in, and the compiler hides all the details from users. They only need to understand the syntax and meaning of operators like +, -, *, and /. Encapsulation is the process of hiding implementation details (data hiding). 2.1.1.2 Common ways to represent ADT a) VDM The Vienna Development Method (VDM) is a model-oriented formal method for the development of computer-based systems and software that has been around for a long time. It is made up of a set of mathematically well-founded languages and tools for expressing and analyzing system models during the early stages of design, before making costly implementation commitments. The model's development and analysis aid in identifying areas of ambiguity or incompleteness in informal system requirements, as well as providing some assurance that a valid implementation will have key properties, particularly those related to safety and security. This is a formal specification language for object-oriented systems. In reality, a formal language likes VDM can specify ADT. b) Table A table is a collection of items that each contain several pieces of information. A search key is one of these pieces of information.

item) the end A = [1, 3, 5] add(5); -> A = [1, 3, 5, 7] 2 void add(int pos, int item) Add int -> void Moving elements originally in positions pos through size() - 1 one place to the right to make space (returning error when pos is less than 0 or larger than size()) and adding new element at position pos in the List. A = [1, 3, 5, 7] add(1, 2); -> A = [1, 2, 3, 5, 7] 3 boolean contains(int item) Find int -> bool If the element is in the List (i.e., there is an item x in the List that equals item), then returning true. A = [1, 2, 3, 5, 7] contains(0); -> false contains(1); -> true 4 int size() Return size int -> int Returning the number of elements in the List. A = [1, 2, 3, 5, 7] size(); - > 5 5 boolean isEmpty() Check empty int -> bool If the ArrayList is empty, returning true. A = [1, 2, 3, 5, 7] isEmpty(); -> false 6 int get(int pos) Get int -> int Returning the element in the List at position pos (erroneous if pos is less than 0 or larger than or equal to size()). A = [1, 2, 3, 5, 7] -> get(0); -> 1 7 int remove(int pos) Remove int -> int Moving elements previously in places pos + 1 through size() -1 one place to the left for filling in the gap (returning error if pos is smaller than 0 or bigger than or equal to size()), removing and returning the element at position pos in the ArrayList.

A = [1, 2, 3, 5, 7]

remove(0); -> 1 -> A = [2, 3, 5, 7] Table 1 : Operations of an arraylist with Vienna Development Method Below is the basic code to manipulate an arraylist including some functions such as:

  • Adding an element to the end of the created arraylist.
  • Adding a new element to a specific position.
  • Sorting elements.
  • Removing the element with a specific index.
  • Finding elements carrying the data which is equal to a specific value.
  • Finding elements that fall within a certain range of values. package ArrayList; import java.util.ArrayList; import java.util.Comparator; import java.util.Random; import java.util.Scanner; public class AList { ArrayList A = new ArrayList(); void Input(int n) { Scanner sc = new Scanner(System.in); Random rd = new Random(); for(int i = 0; i < n; i++) { A.add(rd.nextInt(10)); //Add random number } }

void DeleteAElement(int pos) { System.out.println("The array after deleting element at index " + pos); A.remove(pos); } void Find(int n) { if(A.contains(n)) { System.out.print("The array contains " + n + " at the position: "); for(int i = 0; i < A.size(); i++) { if(A.get(i) == n) { System.out.print(i + ", "); } } } else System.out.println("The array does not contain: " + n); } void Count(int x, int y) { System.out.println("\n\nAll elements that is greater than "

  • x + " and smaller than " + y + " are: "); for(int i = 0; i < A.size(); i++) {

if(A.get(i) > x & A.get(i) < y) { System.out.print(A.get(i) + ", "); } } } public static void main(String[] args) { AList AL = new AList(); AL.Input(10); System.out.println("The initial array:"); AL.Output(); AL.A.sort(null); System.out.println("The array after sorting:"); AL.Output(); AL.Add(13); AL.Output(); AL.Insert(12,5); AL.Output(); AL.DeleteAElement(5); AL.Output(); AL.Find(1); AL.Count(0, 10); AL.DeleteAll(5); AL.Output(); } } Along with that, the image below specifically describes the results received after executing the code mentioned above.

the depth of the stack to be written in its place, moving all stored data one position towards the top of the stack. As a consequence, it is the Last-In-First-Out (LIFO) kind. According to LIFO, the data that was inserted or saved last in a stack must be the first to be removed. In the next diagram, operations on a stack are shown. Figure 3 : Last In First Out (LIFO) Pushing a new element onto the stack, popping an element from the stack, and checking if the stack is empty are the three fundamental operations on a stack. Figure 4 : Pushing and popping in stack

Every function call is represented by a stack frame, also known as an activation record. When a function is called, it is allocated, and once it returns, it is de-allocated. Normally, the stack grows downward on most devices. Additionally, the stack pointer (SP) points to the stack's last element. The stack pointer is placed in the ESP (Extended Stack Pointer) register on x architectures. Further, each stack frame gives memory for arguments, the returned value, and local variables of a function, as well as some admin stuff. Using offsets, the frame pointer provides a starting point for locating local variables. It is kept in the EBP (Extended Base Pointer) register on x86 architectures. Figure 5 : Frame pointer (EBP) and stack pointer (ESP) The return address (in other words, where to restart execution after returning) and previous frame pointer (to identify previous frame) are among the admin stuff that is placed on the stack.

to it. Because of these disadvantages, arrays are less commonly used. Instead of using arrays, the ArrayList class, which addresses all of these issues, can be utilized in this circumstance. The list of students is defined as an arraylist, which means it can be resized. Because the size of the student list is not fixed because new students must be added from time to time or some students must be removed from the list when they join the class, ArrayList can match the above requirements thanks to its dynamic growth and shrinking capabilities. The student management program using an arraylist is programmed as the code below. Specifically, the following tasks are performed by the program:

  • Displaying the menu with the particular options. Depending on the choice of the user that performs the corresponding work.
  • Showing all students along with their specific information.
  • Showing all students who satisfy a certain condition (age greater than 20).
  • Adding students to the list.
  • Deleting students from the list.
  • Sorting the list of students by name.
  • Searching for a student in the list. package StudentManagement; import java.util.ArrayList; import java.util.Comparator; import java.util.Random; import java.util.Scanner; import java.util.Collections; public class Student { ArrayList students = new ArrayList(); int ID; String name; int age; String gender;

String email; Student(){} void Initial() { for (int i = 0; i < 5; i++) { Student stu = new Student(); stu.ID = i; stu.name = "student" + i; stu.email = "student" + i + "@gmail.com"; if (i % 2 == 0) { stu.gender = "Male"; } else { stu.gender = "Female"; } Random rd = new Random(); stu.age = 18 + rd.nextInt(10); students.add(stu); } } String ToString() { return "Student ID:" + ID + "\tName:" + name + "\tGender:"

  • gender + "\tAge:" + age + "\tEmail: " + email; } void PrintList() { System.out.println();