Minimal Network
The following undirected network consists of seven vertices and twelve edges with a total weight of 243.
The same network can be represented by the matrix below.
|
A |
B |
C |
D |
E |
F |
G |
A |
- |
16 |
12 |
21st |
- |
- |
- |
|
B |
16 |
- |
- |
17 |
20 |
- |
- |
|
C |
12 |
- |
- |
28 |
- |
31 |
- |
|
D |
21st |
17 |
28 |
- |
18 |
19 |
23 |
|
E |
- |
20 |
- |
18 |
- |
- |
11 |
|
F |
- |
- |
31 |
19 |
- |
- |
27 |
|
G |
- |
- |
- |
23 |
11 |
27 |
- |
|
However, it's possible to optimise the network by removing some edges and still ensure that all points on the network REM Ain connected. The network which achieves the maximum saving is shown below. It has a weight of representing a saving of 243? The original network.
Using Network.txt (Right click and ' Save link/target as ... '), a 6K text file containing a network with forty vertices, and g Iven in matrix form, find the maximum saving which can is achieved by removing redundant edges whilst ensuring that the NE Twork remains connected.
Minimum network
The following non-forward network contains 7 vertices and 12 edges with a total weight of 243.
This network can also be represented in the form of a matrix as follows.
|
A |
B |
C |
D |
E |
F |
G |
A |
- |
16 |
12 |
21st |
- |
- |
- |
|
B |
16 |
- |
- |
17 |
20 |
- |
- |
|
C |
12 |
- |
- |
28 |
- |
31 |
- |
|
D |
21st |
17 |
28 |
- |
18 |
19 |
23 |
|
E |
- |
20 |
- |
18 |
- |
- |
11 |
|
F |
- |
- |
31 |
19 |
- |
- |
27 |
|
G |
- |
- |
- |
23 |
11 |
27 |
- |
|
However, we can actually optimize the network, remove some of these edges, and still ensure that each vertex is connected to each other. The most weight-saving network, as shown, has a total weight of 93, saving 243 compared to the original network. 93 = 150.
In this 6K text file Network.txt (right click and select "Save Target as ...") a connectivity matrix containing a network of 40 vertices is stored. Remove the redundant edges, while still ensuring that each vertex is connected to the maximum possible weight savings.
Solving
Prim algorithm or Kruskal algorithm
Java
PackageLevel4;ImportJava.io.BufferedReader;Importjava.io.FileNotFoundException;ImportJava.io.FileReader;Importjava.io.IOException;Importjava.util.ArrayList;Importjava.util.Arrays;ImportJava.util.TreeMap; Public classpe0107{Static int[] Network =New int[40] [40]; Static intLen = 40; Static intAllsum=0; Static intShortestsum =0; Public Static voidrun () {String filename= "Src/level4/p107_network.txt"; ReadData (filename); Allsum=getarrsum (); System.out.println ("All elements of and:" +allsum); Shortestsum=Prim (); System.out.println ("Shortest path of the and:" +shortestsum); System.out.println ("Path difference:" + (Allsum-shortestsum)); } Public Static intPrim () {ArrayList<Integer> known =NewArraylist<integer>(); ArrayList<Integer> goods =NewArraylist<integer>(); TreeMap<integer, integer> map =NewTreemap<integer, integer>(); Known.add (0); Map.put (0, 0); while(true){ intMin =Integer.max_value; intindex =-1; intMapindex =-1; for(intI=0;i<known.size (); i++){ for(intj=0;j< len;j++){ intnow =Network[map.get (i)][j]; if(Now < min && now!=-1 &&!)Known.contains (j)) {min=Now ; Index=J; Mapindex=known.size (); }}} goods.add (min); Known.add (index); Map.put (Mapindex,index); if(known.size () = =len) Break; } intSum =0; for(intI=0;i<goods.size (); i++) {sum+=Goods.get (i); } returnsum; } Public Static intgetarrsum () {intsum = 0; for(inti=0;i<network.length;i++){ for(intj=i+1;j<network[0].length;j++){ if(Network[i][j]!=-1) Sum+=Network[i][j]; } } returnsum; } Public Static voidStrtoarr (intindex,string Line) {string[] Strarr= Line.split (","); for(inti=0;i<strarr.length;i++){ if(!strarr[i].equals ("-") ) {Network[index][i]=Integer.parseint (Strarr[i]); }Else{Network[index][i]=-1; } } } Public Static voidreaddata (String filename) {BufferedReader bufferedreader; intindex = 0; Try{BufferedReader=NewBufferedReader (Newfilereader (filename)); String Line=""; Try { while(line = Bufferedreader.readline ())! =NULL) {Strtoarr (index,line); Index++; } } Catch(IOException e) {//TODO auto-generated Catch blockSYSTEM.OUT.PRINTLN ("File has no data"); } } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockSystem.out.println ("No Files Found"); } } Public Static voidMain (string[] args) {LongT0 =System.currenttimemillis (); Run (); LongT1 =System.currenttimemillis (); Longt = T1-t0; System.out.println ("Running Time=" +t/1000+ "s" +t%1000+ "MS"); }}
Project Euler 107:minimal Network minimal networks