Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

COMP108 Data Structures and Algorithms Assignment 2 Deadline: Thursday 3rd May 2018, 4:00pm �� ��Important: Please read all instructions carefully before starting the assignment. Basic information •...

1 answer below »
COMP108 Data Structures and Algorithms
Assignment 2
Deadline: Thursday 3rd May 2018, 4:00pm
�� ��Important: Please read all instructions carefully before starting the assignment.
Basic information
• Assignment: 2 (of 2)
• Deadline: Thursday 3rd May 2018 (Week 11), 4:00pm
• Weighting: 20%
• Electronic Submission:
https:
sam.csc.liv.ac.uk/COMP/Submissions.pl?qryAssignment=COMP108-2
• What to submit: three java files named A2Node.java, A2List.java and A2Graph.java
• Learning outcomes assessed:
– Be able to apply the data structures a
ays and linked lists and their associated algo-
ithms
– Be able to apply a given pseudo code algorithm in order to solve a given problem
– Be able to apply the iterative algorithm design principle
• Marking criteria:
– Co
ectness: 90%
– Programming style: 10%
• There are two parts of the assignment (Part 1: 55% & Part 2: 35%). You are expected to
complete both.
1 Part 1: The List Accessing Problem (55%)
Suppose we have a filing cabinet containing some files with (unsorted) IDs. We then receive a
sequence of requests for certain files, given in the IDs of the files. Upon receiving a request of ID,
say key, we have to locate the file by flipping through the files in the cabinet one by one. If we
find key, it is called a hit and we remove the file from the cabinet. Suppose key is the i-th file
in the cabinet, then we pay a cost of i, which is the number of comparisons required. If key is
not in the cabinet, the cost is the number of files in the cabinet and we then go to a storage to
locate the file. After using the file, we have to return the file back to the cabinet at the original
location or we may take this chance to reorganise the file cabinet, e.g., by inserting the requested
file to the front. As the file can only be accessed one after another, it is sensible to use the data
structure linked list to represent the file cabinet.
1
1.1 The algorithms
We consider three accessing
eorganising algorithms. To illustrate, we assume the file cabinet
initially contains 3 files with IDs XXXXXXXXXXand the sequence of requests is XXXXXXXXXX.
• Append if miss: This algorithm does not reorganise the file cabinet and only appends a
file at the end if it was not originally in the cabinet. Therefore, there will be 5 hits, the file
cabinet will become XXXXXXXXXXat the end, and the number of comparisons (cost) for the
6 requests is respectively XXXXXXXXXXThe following table illustrates every step.
equest list beforehand hit? # comparisons list afterward
XXXXXXXXXXyes 1 no change
XXXXXXXXXXyes 2 no change
XXXXXXXXXXno XXXXXXXXXX
XXXXXXXXXXyes 2 no change
XXXXXXXXXXyes 4 no change
XXXXXXXXXXyes 1 no change
• Move to front: This algorithm moves the file just requested (including newly inserted one)
to the front of the list. In this case, there will be 5 hits. The file cabinet will become 20 5
30 10 at the end. The number of comparisons (cost) for the 6 requests is respectively 1 2
XXXXXXXXXXThe following table illustrates every step.
equest list beforehand hit? # comparisons list afterward
XXXXXXXXXXyes 1 no change
XXXXXXXXXXyes XXXXXXXXXX
XXXXXXXXXXno XXXXXXXXXX
XXXXXXXXXXyes XXXXXXXXXX
XXXXXXXXXXyes XXXXXXXXXX
XXXXXXXXXXyes XXXXXXXXXX
• Frequency count: This algorithm rea
anges the files in non-increasing order of frequency
of access. This means that the algorithm keeps a count of how many times a file has been
equested. When a file is requested, its counter get increased by one and it needs to be
moved to the co
ect position. If there are other files with the same frequency, the newly
equested file should be put behind those with the same frequency. We assume that the
files initially in the cabinet has frequency of 1 to start with. A newly inserted file also has
a frequency of 1.
In this case, there will be 5 hits. The file cabinet will become XXXXXXXXXXat the end. The
number of comparisons (cost) for the 6 requests is respectively XXXXXXXXXXThe following
table illustrates every step.
equest list beforehand hit? # comparisons list afterward frequency count afterward
XXXXXXXXXXyes 1 no change 2 1 1
XXXXXXXXXXyes 2 no change 2 2 1
XXXXXXXXXXno XXXXXXXXXX1
XXXXXXXXXXyes XXXXXXXXXX1
XXXXXXXXXXyes XXXXXXXXXX1
XXXXXXXXXXyes 2 no change XXXXXXXXXX
2
1.2 The programs A2Node.java and A2List.java
Two programs A2Node.java and A2List.java can be downloaded from
http:
www2.csc.liv.ac.uk/~pwong/teaching/comp108/201718/assess.html
The program A2Node.java contains a class called A2Node with the following attributes
public int data;
public A2Node next;
public int freq;
The program A2List.java has declared two global variables head and tail which point to the
head (first node) and the tail (last node), respectively, of the list representing the file cabinet. The
program contains a number of methods already written which you must NOT change, including
• public static void main(String[] args)
• static void insertNodeHead(A2Node newNode) which inserts the node called newNode
to the head of the list
• static A2Node deleteHead() which deletes the node at the head
• static void printList() which prints the list from head to tail
• static void emptyList() which empties the whole list
The main method takes care of the input, reading (i) number of files in the initial cabinet, (ii)
the file IDs in the initial cabinet, (iii) number of file requests, and (iv) IDs of the file requests.
(Notice that the input part has suppressed printing text asking for input.) It then creates a list of
the initial cabinet and calls each algorithm in turn. The order of calling the algorithm has been
pre-determined and you should not change the order, otherwise, you may lose all your marks. The
header of each algorithm has been provided. They can all access global variables head and tail.
1.3 Your tasks
There are three tasks you need to do. Each task is associated with each of the algorithms, and
each should print and only print the following. See Test Cases below for examples.
(i) The number of comparisons for each request separated by a space , e.g., 1 2 3
(Note: one single space at the end is allowed.)
(ii) x h where x is the number of hits
(iii) The content of the final list in the form List: a b c d
where a b c d are the IDs of file in the final list. (Again one single space at the end is
allowed.) If your list is maintained properly the method printList will do the job.
• Task XXXXXXXXXX%) Implement the appendIfMiss() method that appends a missed file to the
end of the list and does not reorganise the list.
• Task XXXXXXXXXX%) Implement the moveToFront() method that moves a requested file o
inserts a missed file to the front of the list. When moving a node in the list, you have to
make sure that the next field of affected nodes, head, and tail are updated properly.
• Task XXXXXXXXXX%) Implement the freqCount() method that moves a requested file or inserts a
missed file in a position such that the files in the list are in non-increasing order. Importantly
when the requested file has the same frequency count as other files, the request file should
e placed at the end among them. Again make sure you update next, head, tail properly.
3
1.4 Test cases
Below are some sample test cases and the expected output. These test cases can be downloaded
as listTest01.txt, listTest02.txt, listTest03.txt on the assessment page: http:
www2.csc.liv.
ac.uk/~pwong/teaching/comp108/201718/assess.html
You can run the program easier by typing java A2List < listTest01.txt in which case you
don’t have to type the input over and over again.
Make sure you remove or comment print statements that you use for debugging before sub-
mission. The order of output should be appendIfMiss, moveToFront, freqCount. Your program
will be tested in this order, therefore, do NOT change the order (you should not change the main
method anyway)! Your program will be marked by five other test cases that have not be revealed.
Test case Input Output (‘ ’ represents a space)
#1 3
XXXXXXXXXX
6
XXXXXXXXXX
appendIfMiss...
XXXXXXXXXX
5 h
List: XXXXXXXXXX
moveToFront...
XXXXXXXXXX
5 h
List: XXXXXXXXXX
freqCount...
XXXXXXXXXX
5 h
List: XXXXXXXXXX
#2 4
XXXXXXXXXX
9
XXXXXXXXXX 40 70
appendIfMiss...
XXXXXXXXXX
7 h
List: XXXXXXXXXX
moveToFront...
XXXXXXXXXX
7 h
List: XXXXXXXXXX
freqCount...
XXXXXXXXXX
7 h
List: XXXXXXXXXX
#3 3
XXXXXXXXXX
20
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
appendIfMiss...
XXXXXXXXXX XXXXXXXXXX
17 h
List: XXXXXXXXXX
moveToFront...
XXXXXXXXXX XXXXXXXXXX
17 h
List: XXXXXXXXXX
freqCount...
XXXXXXXXXX XXXXXXXXXX
17 h
List: XXXXXXXXXX
2 Part 2: Distance Neighbourhood on Graphs (35%)
Suppose we have an undirected graph to model facebook connection (or some other similar social
network). There are n users, each represented by a vertex. If two users are friends of each other,
then there is an edge between the two co
esponding vertices. This forms a simple graph (at
most one edge between any two vertices) with no self-loop (a vertex has no edge to itself). This
friend-relationship can be represented by an adjacency matrix of size n× n
Answered Same Day May 02, 2020

Solution

Snehil answered on May 06 2020
145 Votes
A2Graph.java
A2Graph.java
*
    Name:
    Student ID:
    Departmental username:
    University email address;
*
import java.util.*;
import java.io.*;
class A2Graph {
    private static final int MaxVertex = 100;
    private static final int MinVertex = 1;
    private static Scanner keyboardInput = new Scanner(System.in);
    
 adjacency matrix, adjMatrix[i][j] = 1 means i and j are adjacent, 0
    
 otherwise
    public static int[][] adjMatrix = new int[MaxVertex][MaxVertex];
    public static int numVertex; 
 total number of vertices
    
 DO NOT change the main method
    public static void main(String[] args) throws Exception {
        boolean userContinue = true;
        int distance = 1;
        int[][] neighbourMatrix = new int[MaxVertex][MaxVertex];
        input();
        try {
            
 System.out.print("Enter a distance (" + MinVertex + "--" +
            
 numVertex + ", -1 to exit): ");
            distance = keyboardInput.nextInt();
        } catch (Exception e) {
            keyboardInput.next();
        }
        if (distance < MinVertex || distance > numVertex)
            System.out.println("inco
ect range");
        else {
            neighbourhood(distance, neighbourMatrix, numVertex);
            printSquareA
ay(neighbourMatrix, numVertex);
        }
        degreeSeparation();
    }
    
 find the degree of separation of the graph using the method
    
 neightbourhood()
    static void degreeSeparation() {
        int[][] neighbourMatrix = new int[MaxVertex][MaxVertex];
        neighbourhood(numVertex, neighbourMatrix, numVertex);
        boolean connected = true;
        int degree = 0;
        for (int i = 0; i < numVertex && connected == true; i++) {
            for (int j = 0; j < numVertex; j++) {
                if (i == j) {
                    continue;
                } else if (neighbourMatrix[i][j] == 0) {
                    connected = false;
                    
eak;
                } else {
                    degree = Integer.max(degree, neighbourMatrix[i][j]);
                }
            }
        }
        if (connected) {
            System.out.println("Degree of separation is " + degree);
        } else {
            System.out.println("The graph is not connected");
        }
    }
    
 input parameter: an integer distance
    
 output: compute neighbourhood matrix for distance
    static void neighbourhood(int distance, int result[][], int size) {
        for (int i = 0; i < size; i++) {
            int dist[] = new int[MaxVertex];
            for (int j = 0; j < size; j++) {
                
 result[i][j] = Integer.max(result[i][j], adjMatrix[i][j]);
                for (int k = 0; k < size; k++) {
                    if (i == k)
                        dist[k] = 0;
                    else
                        dist[k] = Integer.MAX_VALUE;
                }
            }
            boolean list[] = new boolean[MaxVertex];
            for (int l = 0; l < size - 1; l++) {
                int min = distance + 1, u = 0;
                for (int v = 0; v < size; v++) {
                    if (list[v] == false && dist[v] <= min) {
                        min = dist[v];
                        u = v;
                    }
                }
                list[u] = true;
                for (int v = 0; v < size; v++) {
                    if (list[v] == false && adjMatrix[u][v] != 0
                            && dist[u] != Integer.MAX_VALUE
                            && (dist[u] + adjMatrix[u][v] < dist[v])) {
                        dist[v] = dist[u] + adjMatrix[u][v];
                    }
                }
            }
            for (int j = 0; j < size; j++) {
                if (dist[j] > distance) {
                    result[i][j] = 0;
                } else {
                    result[i][j] = dist[j];
                }
            }
        }
    }
    
 DO NOT change this method
    static void printSquareA
ay(int a
ay[][], int size) {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                System.out.print(a
ay[i][j] + " ");
            }
            System.out.println();
        }
    }
    
 DO NOT change this method
    static void input() {
        int i, j;
        boolean success = false;
        try {
            success = true;
            
 System.out.print("How many vertices (" + MinVertex + "--" +
            
 MaxVertex + ")? ");
            numVertex = keyboardInput.nextInt();
            if (numVertex > MaxVertex || numVertex < MinVertex) {
                success = false;
            }
            if (success) {
                
 System.out.printl...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here