Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Computer Science 320S2C (2022)

Assignment 1 (Graph Algorithms)

Introduction

This warm-up assignment tests your pre-requisite skills from CS220 on graph optimization problems. You need to submit three programs for the following tasks to https://www .automarker .cs .auckland .ac .nz/ to get marks for this assignment. Most common programming languages will be available to do the assignment. Note this marker runs on a linux box so no Microsoft specific code should be used; read the help/FAQ for more details. Input is from keyboard/stdin and output is to console/stdout.

Task 1 is worth 2 marks and you can implement any standard solution.  Tasks 2 is worth 3 marks, where some algorithm design thinking is required.  Both problems will contain easy and harder input cases on the automarker.

An excessive number of submissions (over 10) for a particular problem will accrue a 20% penalty per that problem if you eventually solve it.

Task 1: Minimum Spanning Trees

For this warm-up task you are to implement any efficient minimum spanning tree algorithm that takes a sequence of edge-weighted graphs and outputs the minimum cost weight of a spanning tree of each.

Input Format

For this assignment we use adjacency matrices with positive integer weights.  Here a zero entry at row i and column j indicates that no edge ij exists in the graph. The rst line consists of an integer n < 1000 denoting the order of the graph. This is then followed by n lines of n white-space separated integers denoting edge weights. The sequence of graphs is terminated by a value n = 0, which is not processed.

3

0  1  3

1  0  2

3  2  0

4

0  2  7  0

2  0  5  1

7  5  0  3

0  1  3  0

6

0  1  0  4  0  0

1  0  3  0  3  0

0  3  0  0  0  2

4  0  0  0  2  0

0  3  0  2  0  1

0  0  2  0  1  0

0

Output Format

Output should be one line for each input graph indicating the minimum cost weighted tree. The sample output for the previous input cases is as follows.

3

6

Task 2:  Snakes in a Graph

For a graph G = (V, E), a snake (also called an npdaced taui) is a path v1 , v2 , . . . , vk  such that for all j - i > 1 (vi , vj ) e\ E . That is, it is a sequence of vertices in G such that each two adjacent vertices in the sequence are connected by an edge in G, and each two nonadjacent vertices in the sequence are not connected by any edge in G.

For this task, our goal is to determine the longest snake of a graph.

Input Format

Input for this problem consist of a sequence of one or more (undirected) graphs taken from the keyboard. Each graph is represented by an adjacency list. The rst line is an integer n indicating the order of the graph. This is followed by n white space separated lists of adjacencies for nodes labeled 0 to n - 1.   The input will be terminated by a line consisting of one zero (0).  This line should not be processed.  Two sample input graphs are listed below. The easy (harder) test cases are graphs of order at most 10 (50).

4

1

0  3

1

5

1  4

0  2

1  3  4

2  4

0  2  3

0

Output Format

Output will be just one integer per line.   For the above, input we would output the following two integers denoting the longest snake of the two graphs. Recall that the length of a path is the number of edges.

2

3