Your project will not be graded if You do not produce outputs for all the test cases by executing your source code Your source code is not included in the required extensions (*.c, *.cpp, *.java, or...

1 answer below »


Your project will not be graded if




  • You do not produce outputs for all the test cases by executing your source code


  • Your source code is not included in the required extensions (*.c, *.cpp, *.java, or *.py)


  • Your report (PDF file) does not include results for all test cases


  • Your report is in some other format than PDF



Problem 1 (30 points)


Perform the following by reading in two graphs (networks), G1 (Project3_G1_Data.csv) and G2 (Project3_G2_Data.csv):



  • Design and implement an algorithm to merge these two input graphs into one graph (network), called G. Your algorithm must create a new adjacency matrix representing the merged graph (G) in two ways: one by using a two-dimensional array and another by using a linked-list.

  • Implement Kruskal’s algorithm in the textbook to find a minimum-spanning tree in graph G.

  • Print the computed minimum-spanning tree as you did in Quiz 6 and Quiz 7.

  • Report on the memory usage by each of the two data structures.



Submission Items



  • Three programs (one for merging graphs and two for finding a minimum-spanning tree, one for each data structure)

  • A PDF file containing

    • The result of the computed minimum-spanning tree and the total distance on it

    • The analysis of the memory usage by each data structure





Problem 2 (70 points)


Design and implement your own algorithm to perform K-Means clustering. Your algorithm must implement methods for handling centroid initialization, calculating distance, assigning centroids, moving centroids, calculating loss, and stopping criteria (see Resources below). You must run your algorithm on two different datasets (see below). The first dataset, test case, is synthetic data designed for the purpose of checking the result of your algorithm before running your algorithm on the second dataset which is real data about electric power consumption.



Requirements for Test Case Dataset (
P
roject3_Test_
Case.csv
)


This dataset contains three variables: X1, X2, and Y. Use X1 and X2 to perform clustering. Use Y as the ground truth in your experiments. Run the algorithm with K=2 and report on the following:



  • The coordinates of each centroid

  • A count of the number of points assigned to each centroid

  • The losses for each iteration of training



Requirements for Electric Power Consumption Dataset (
Project3_Power_Consump
tion.csv
)


This dataset provides samples for individual household electric power consumption. For a description of the dataset checkhere(Links to an external site.). Perform K-Means clustering using all variables in this dataset. Report on the following for the different values of K.



  • For K=2

    • The coordinates of each centroid

    • A count of the number of points assigned to each centroid

    • The losses for each iteration of training



  • For K from 3 to 20

    • A scatter plot containing the loss of the last iteration of training for each value of K

    • The coordinates of each centroid for each value of K

    • The number of points assigned to each centroid for each value of K

    • Using the elbow method (see Resources) find the value of K which best fits the data





Additional Requirements



  • Explain the stopping criteria you selected for your algorithm (see Resources) and how they may impact your results.

  • Explain the centroid initialization method you selected and how that may impact your results.

  • Explain why your algorithm may require different amounts of time to run on the same input and parameters.

  • Explain why your algorithm may produce different results on the same input and parameters.



Resources



Project3_Resources.pdf

Preview the document



Submission Items



  • Two programs (one for each dataset) that output the centroid coordinates, loss, and number of points assigned to each centroid by your algorithm for K=2

  • A PDF file with the required information for each dataset and the responses to the additional requirements



Quiz 8 (March 18, 2021)" wfd-id="224" style="float: left;">Previous

Answered 13 days AfterMar 18, 2021

Answer To: Your project will not be graded if You do not produce outputs for all the test cases by executing...

Kushal answered on Mar 22 2021
134 Votes
Question 1/ms (1) (1).java
Question 1/ms (1) (1).java
import java.util.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ms {

public static void main(String[] args) {
            ms graph = new ms();
            String line = "";
            String splitBy = ","; 
            ArrayList graphEdges = new ArrayList(); 
            try
            {  
            //parsing a CSV file into BufferedReader class constructor
            BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\sonid\\Downloads\\downloads-2jqszhwd-i520iikm (1)\\project3g1data-1-521hapwl.csv"));
            while ((line = br.readLine()) != null)   //returns a Boolean value
            {  
 
                String[] employee = line.split(splitBy);
                String a =employee[0];
                String b = employee[1];
                String c = employee[2];
 
                int inum1 = Integer.pars
eInt(a);
                int inum2 = Integer.parseInt(b);
                int inum3 = Integer.parseInt(c);
 
                graphEdges.add(new Edge(inum1,inum2,inum3));
 
 
 

 
            }
            }
 
            catch (IOException e) 
            {  
            e.printStackTrace();
            }
 
 
            int nodeCount = 1468;
            graph.kruskalMST(graphEdges, nodeCount);
}
        public void kruskalMST(ArrayList graphEdges, int nodeCount){
            String outputMessage="";
            Collections.sort(graphEdges);       //sort edges with smallest weight 1st
            ArrayList mstEdges = new ArrayList();   //list of edges included in the Minimum spanning tree (initially empty)
            DisjointSet nodeSet = new DisjointSet(nodeCount+1);     //Initialize singleton sets for each node in graph. (nodeCount +1) to account for arrays indexing from 0
            for(int i=0; i                Edge currentEdge = graphEdges.get(i);
                int root1 = nodeSet.find(currentEdge.getVertex1());     //Find root of 1 vertex of the edge
                int root2 = nodeSet.find(currentEdge.getVertex2());
                outputMessage+="find("+currentEdge.getVertex1()+") returns "+root1+", find("+currentEdge.getVertex2()+") returns "+root2;       //just print, keep on same line for union message
                String unionMessage=",\tNo union performed\n";      //assume no union is to be performed, changed later if a union DOES happen
                if(root1 != root2){         //if roots are in different sets the DO NOT create a cycle
                    mstEdges.add(currentEdge);      //add the edge to the graph
                    nodeSet.union(root1, root2);    //union the sets
                    unionMessage=",\tUnion("+root1+", "+root2+") done\n";       //change what's printed if a union IS performed
                }
                outputMessage+=unionMessage;
            }
            outputMessage+="\nFinal Minimum Spanning Tree ("+mstEdges.size()+" edges)\n";
            int mstTotalEdgeWeight=0;
            for(Edge edge: mstEdges){
                outputMessage+=edge +"\n";      //print each edge
                mstTotalEdgeWeight += edge.getWeight();
            }
            outputMessage+="\nTotal weight of all edges in MST = "+mstTotalEdgeWeight;
            System.out.println(outputMessage);
            try (PrintWriter outputFile = new PrintWriter( new File("06outputMST.txt") ); ){
                outputFile.println(outputMessage);
                System.out.println("\nOpen \"06outputMST.txt\" for backup copy of answers");
            } catch (FileNotFoundException e) {
                System.out.println("Error! Couldn't create file");
            }
        }
    }
    class Edge implements Comparable{
        private int vertex1;    //an edge has 2 vertices & a weight
        private int vertex2;
        private int weight;
        public Edge(int vertex1, int vertex2, int weight){
            this.vertex1=vertex1;
            this.vertex2=vertex2;
            this.weight=weight;
        }
        public int getVertex1(){
            return vertex1;
        }
        public int getVertex2(){
            return vertex2;
        }
        public int getWeight(){
            return weight;
        }
        @Override
        public int compareTo(Edge otherEdge) {              //Compare based on edge weight (for sorting)
            return this.getWeight() - otherEdge.getWeight();
        }
        @Override
        public String toString() {
            return "Edge ("+getVertex1()+", "+getVertex2()+") weight="+getWeight();
        }
    }
    // DisjointSet class
    //
    // CONSTRUCTION: with int representing initial number of sets
    //
    // ******************PUBLIC OPERATIONS*********************
    // void union(root1, root2) --> Merge two sets
    // int find(x)              --> Return set containing x
    // ******************ERRORS********************************
    // No error checking is performed
    // http://users.cis.fiu.edu/~weiss/dsaajava3/code/DisjSets.java
    /**
     * Disjoint set class, using union by rank and path compression
     * Elements in the set are numbered starting at 0
     * @author Mark Allen Weiss
     */
    class DisjointSet{
        private int[] set;      //the disjoint set as an array
        public int[] getSet(){      //mostly debugging method to print array
            return set;
        }
        /**
         * Construct the disjoint sets object.
         * @param numElements the initial number of disjoint sets.
         */
        public DisjointSet(int numElements) {       //constructor creates singleton sets
            set = new int [numElements];
            for(int i = 0; i < set.length; i++){        //initialize to -1 so the trees have nothing in them
                set[i] = -1;
            }
        }
        /**
         * Union two disjoint sets using the height heuristic.
         * For simplicity, we assume root1 and root2 are distinct
         * and represent set names.
         * @param root1 the root of set 1.
         * @param root2 the root of set 2.
         */
        public void union(int root1, int root2) {
            if(set[root2] < set[root1]){        // root2 is deeper
                set[root1] = root2;     // Make root2 new root
            }
            else {
                if(set[root1] == set[root2]){
                    set[root1]--;           // Update height if same
                }
                set[root2] = root1;     // Make root1 new root
            }
        }
        /**
         * Perform a find with path compression.
         * Error checks omitted again for simplicity.
         * @param x the element being searched for.
         * @return the set containing x.
         */
        public int find(int x) {
            if(set[x] < 0){     //If tree is a root, return its index
                return x;
            }
            int next = x;
            while(set[next] > 0){       //Loop until we find a root
                next=set[next];
            }
            return next;
        }

    }






Question 1/Result (1).pdf
Edges in the constructed MST
Edge Weight
79 - 205 = 16
477 - 511 = 25
530 - 867 = 25
784 - 785 = 25
585 - 587 = 26
50 - 404 = 27
157 - 188 = 27
635 - 734 = 27
656 - 657 = 27
393 - 962 = 29
180 - 506 = 30
413 - 780 = 30
545 - 1084 = 30
699 - 700 = 30
485 - 504 = 31
615 - 908 = 31
394 - 503 = 32
309 - 473 = 32
60 - 98 = 33
239 - 429 = 33
218 - 246 = 33
412 - 944 = 34
507 - 623 = 34
184 - 1051 = 35
670 - 1083 = 35
536 - 717 = 36
486 - 639 = 37
627 - 628 = 38
711 - 794 = 40
195 - 198 = 42
23 - 1399 = 42
595 - 1436 = 42
50 - 1391 = 43
239 - 240 = 43
455 - 719 = 43
334 - 431 = 44
534 - 1074 = 44
282 - 347 = 45
451 - 464 = 45
100 - 463 = 46
243 - 469 = 46
29 - 231 = 46
490 - 502 = 47
541 - 810 = 47
883 - 918 = 47
77 - 714 = 48
888 - 889 = 49
147 - 202 = 50
786 - 787 = 50
799 - 848 = 50
196 - 344 = 51
681 - 1361 = 51
290 - 1011 = 52
438 - 843 = 52
695 - 696 = 52
771 - 1041 = 52
20 - 21 = 53
160 - 296 = 53
483 - 1097 = 53
94 - 114 = 54
411 - 533 = 54
178 - 180 = 55
353 - 446 = 56
340 - 452 = 57
489 - 501 = 57
347 - 384 = 58
214 - 248 = 58
371 - 446 = 58
623 - 625 = 58
687 - 955 = 58
769 - 859 = 58
873 - 1003 = 58
160 - 407 = 59
738 - 1026 = 59
75 - 275 = 60
303 - 304 = 60
754 - 755 = 60
357 - 494 = 61
335 - 548 = 61
365 - 366 = 61
145 - 476 = 62
55 - 721 = 63
551 - 723 = 63
768 - 1370 = 63
853 - 854 = 65
190 - 287 = 67
81 - 1257 = 67
427 - 428 = 67
507 - 508 = 67
243 - 244 = 68
795 - 799 = 68
84 - 85 = 69
370 - 401 = 69
797 - 877 = 69
4 - 5 = 70
179 - 239 = 70
890 - 891 = 70
315 - 530 = 71
290 - 983 = 72
179 - 725 = 73
489 - 1151 = 73
15 - 743 = 74
203 - 528 = 75
749 - 1395 = 75
261 - 390 = 76
15 - 909 = 76
537 - 1428 = 76
720 - 1403 = 76
851 - 983 = 76
853 - 857 = 76
2 - 4 = 77
85 - 168 = 78
361 - 508 = 78
422 - 423 = 79
701 - 702 = 79
704 - 705 = 80
704 - 1092 = 80
821 - 1090 = 80
465 - 468 = 81
325 - 817 = 81
425 - 462 = 82
274 - 456 = 82
683 - 1361 = 82
740 - 883 = 83
779 - 861 = 83
265 - 519 = 84
49 - 123 = 85
230 - 231 = 85
387 - 388 = 85
470 - 471 = 85
659 - 885 = 85
70 - 98 = 86
221 - 492 = 87
401 - 466 = 87
283 - 367 = 87
170 - 171 = 88
884 - 886 = 88
280 - 281 = 89
475 - 505 = 89
32 - 65 = 89
415 - 444 = 89
548 - 1422 = 89
569 - 570 = 89
871 - 872 = 89
240 - 999 = 90
646 - 647 = 90
74 - 322 = 91
451 - 1363 = 92
872 - 1263 = 92
873 - 967 = 92
34 - 91 = 93
52 - 201 = 93
504 - 505 = 93
344 - 481 = 93
349 - 753 = 93
668 - 1089 = 93
505 - 506 = 94
294 - 308 = 94
684 - 779 = 94
33 - 106 = 96
253 - 396 = 96
568 - 569 = 96
581 - 810 = 96
889 - 988 = 96
32 - 69 = 97
71 - 105 = 97
244 - 753 =...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here