After the description of the disjoint set data structure (which I think you should read), it cannot come as a surprise what kind of solution we want to use for problem 107 of Project Euler. However, let as always start with the problem description:

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.

ABCDEFGA–161221–––B16––1720––C12––28–31–D211728–181923E–20–18––11F––3119––27G–––231127–

However, it is possible to optimise the network by removing some edges and still ensure that all points on the network remain connected. The network which achieves the maximum saving is shown below. It has a weight of 93, representing a saving of 243 – 93 = 150 from the original network.

Using network.txt a text file containing a network with forty vertices, and given in matrix form, find the maximum saving which can be achieved by removing redundant edges whilst ensuring that the network remains connected.

Let me say it in this way. If you know that there already exists algorithms for this kind of problem, then it is dead easy. If you don’t then I wouldn’t even know how to start. However, I was in the lucky first category. What we are trying to find is a minimum spanning tree. Which is the smallest set of vertices in a weighted graph, such that we only have one component in the graph (all vertices are connected).

There exists two algorithms which are used to find this tree; Prim’s algorithm and Kruskal’s algorithm and to me the latter seemed like the simplest one to understand and implement, so that is what I used.

From Wikipedia we have the description

*create a forest F (a set of trees), where each vertex in the graph is a separate tree**create a set S containing all the edges in the graph**while S is nonempty and F is not yet spanning**remove an edge with minimum weight from S**if that edge connects two different trees, then add it to the forest, combining two trees into a single tree**otherwise discard that edge.*

*At the termination of the algorithm, the forest has only one component and forms a minimum spanning tree of the graph.*

So all we need now is to implement the algorithm and the input reading around it. Oh… and not least figure out that we should find the saving not the cost of the minimum spanning tree. That took me 10 minutes of debugging.

Using the disjoint data set class we just covered the code looks like

//Read the input file string[] lines = File.ReadAllLines(filename); //create a forest F (a set of trees), //where each vertex in the graph is a separate tree int N = lines[0].Split(',').Length; DisjointSet vertices = new DisjointSet(N); //create a set S containing all the edges in the graph //The tuple contains weight,vertex, vertex List<Tuple<int, int, int>> edges = new List<Tuple<int, int, int>>(); int initialWeight = 0; for(int i = 0; i < N; i++){ string[] edge = lines[i].Split(','); for (int j = 0; j < i; j++) { if (edge[j] != "-") { int weight = Convert.ToInt32(edge[j]); edges.Add(new Tuple<int,int,int>(weight,i,j)); initialWeight += weight; } } } //Sort edges to have the minimum weight at top edges.Sort(); int k = 0; //while S is nonempty and F is not yet spanning int minSpanningTreeWeight = 0; //while(!vertices.isSpanning()){ while (!vertices.isSpanning()) { //remove an edge with minimum weight from S //Since we have a sorted list we just go through the list //if that edge connects two different trees, //then add it to the forest, //combining two trees into a single tree if(vertices.Find(edges[k].Item2) != vertices.Find(edges[k].Item3)){ vertices.Union(edges[k].Item2, edges[k].Item3); minSpanningTreeWeight += edges[k].Item1; } k++; }

Then what remains is to subtract the weight of the spanning tree from the weight of the original graph and we have the following solution (click to see the solution)

The saving is 259679

which runs in

Solution took 2,4353 ms

## Wrapping up

Once you figure out that there are indeed algorithms for this kind of stuff the problem is reduced to something which is a matter of implementation. Something which should be trivial, but I often find myself struggling to find the right way to code the algorithms and data structures. In this problem I had help with the Disjoint-set data structure and that made it almost trivial.

The full source code can be found here.

How did you solve this problem?

I’m solving it using Prim’s Minimal Spanning Tree Algorithm. It’s not working yet though (implementation issues . . . ).

I have been using the Kruskal’s algorithm too but I’m getting 259671 as the answer.It shows the total weight to be 261832 which seems to be correct.If you could post the weights of the edges in the MST,it would be easier for me to check my error! 🙂

Lang:C++

I could do that, but there are a pretty lot of them (513), so would you mind me mailing them to you instead? And what format would you like them in?

Yes sure!You can mail them to me,I don’t mind them at all!

And I don’t know C# so a C/C++ code to print them or a text file containing them or even if you paste them in the body,anything would be fine!

Can you repost the input file network.txt. The link seems to be broken.

I solved this using python and dictionaries. But I had trouble detecting if an edge belong to two different sub trees. That’s where my time consumes.

https://gist.github.com/5636502e56fe57fc0cea.git

It took only 8 seconds.. < 9000 time. Which is apparently faster.

I believe there is a much more efficient way to do this. =)

Prim’s algorithm can actually be run on a table, or in this case, a matrix.

I have posted my Java solution to this problem here:

https://github.com/Thevesh/ProjectEuler/blob/master/Level005/problem107.java

My code runs super fast, as follows:

Cheers, and thanks for your website!

Hi, I believe we don’t need to call isSpanning method at all; since this is MST, edge count should be N – 1; simple variable holding # of edges should be sufficient.

Well I thought the beauty of C++ is the STL. So I implemented the forest as an unordered_set of ‘copses’ each of which is also and unordered_set (hash table) containing the indicies of individual ‘trees’ or ‘nodes’ in the graph. The edges were a vector of a simple class containing the two ends indicies and their distance and a flag to say if it had been selected as part of the MST. Also needed a comparison functor to sort the edge objects by distance.

Ran in about 1ms , I suspect that most of time was sorting the edges into length order, there is more scope here for a speed up, as the distances are all integers in a defined range, so a radix sort (O(n) time) could be used but that is not in the STL. Another optimisation would be to bail out of the main loop when the all the nodes are in only one set and the MST is thus complete and no further edges will be added.

// Euler Project 107

#include

#include

#include

#include

using namespace std;

static const int nBushes = 40;

static const int NoRoad = -1;

static const int adjM[nBushes][nBushes] = {

NoRoad,NoRoad,NoRoad,427,668,495,377,678,NoRoad,177,NoRoad,NoRoad,870,NoRoad,869,624,300,609,131,NoRoad,251,NoRoad,NoRoad,NoRoad,856,221,514,NoRoad,591,762,182,56,NoRoad,884,412,273,636,NoRoad,NoRoad,774,

NoRoad,NoRoad,262,NoRoad,NoRoad,508,472,799,NoRoad,956,578,363,940,143,NoRoad,162,122,910,NoRoad,729,802,941,922,573,531,539,667,607,NoRoad,920,NoRoad,NoRoad,315,649,937,NoRoad,185,102,636,289,

NoRoad,262,NoRoad,NoRoad,926,NoRoad,958,158,647,47,621,264,81,NoRoad,402,813,649,386,252,391,264,637,349,NoRoad,NoRoad,NoRoad,108,NoRoad,727,225,578,699,NoRoad,898,294,NoRoad,575,168,432,833,

427,NoRoad,NoRoad,NoRoad,366,NoRoad,NoRoad,635,NoRoad,32,962,468,893,854,718,427,448,916,258,NoRoad,760,909,529,311,404,NoRoad,NoRoad,588,680,875,NoRoad,615,NoRoad,409,758,221,NoRoad,NoRoad,76,257,

668,NoRoad,926,366,NoRoad,NoRoad,NoRoad,250,268,NoRoad,503,944,NoRoad,677,NoRoad,727,793,457,981,191,NoRoad,NoRoad,NoRoad,351,969,925,987,328,282,589,NoRoad,873,477,NoRoad,NoRoad,19,450,NoRoad,NoRoad,NoRoad,

495,508,NoRoad,NoRoad,NoRoad,NoRoad,NoRoad,765,711,819,305,302,926,NoRoad,NoRoad,582,NoRoad,861,NoRoad,683,293,NoRoad,NoRoad,66,NoRoad,27,NoRoad,NoRoad,290,NoRoad,786,NoRoad,554,817,33,NoRoad,54,506,386,381,

377,472,958,NoRoad,NoRoad,NoRoad,NoRoad,NoRoad,NoRoad,120,42,NoRoad,134,219,457,639,538,374,NoRoad,NoRoad,NoRoad,966,NoRoad,NoRoad,NoRoad,NoRoad,NoRoad,449,120,797,358,232,550,NoRoad,305,997,662,744,686,239,

678,799,158,635,250,765,NoRoad,NoRoad,NoRoad,35,NoRoad,106,385,652,160,NoRoad,890,812,605,953,NoRoad,NoRoad,NoRoad,79,NoRoad,712,613,312,452,NoRoad,978,900,NoRoad,901,NoRoad,NoRoad,225,533,770,722,

NoRoad,NoRoad,647,NoRoad,268,711,NoRoad,NoRoad,NoRoad,283,NoRoad,172,NoRoad,663,236,36,403,286,986,NoRoad,NoRoad,810,761,574,53,793,NoRoad,NoRoad,777,330,936,883,286,NoRoad,174,NoRoad,NoRoad,NoRoad,828,711,

177,956,47,32,NoRoad,819,120,35,283,NoRoad,50,NoRoad,565,36,767,684,344,489,565,NoRoad,NoRoad,103,810,463,733,665,494,644,863,25,385,NoRoad,342,470,NoRoad,NoRoad,NoRoad,730,582,468,

NoRoad,578,621,962,503,305,42,NoRoad,NoRoad,50,NoRoad,155,519,NoRoad,NoRoad,256,990,801,154,53,474,650,402,NoRoad,NoRoad,NoRoad,966,NoRoad,NoRoad,406,989,772,932,7,NoRoad,823,391,NoRoad,NoRoad,933,

NoRoad,363,264,468,944,302,NoRoad,106,172,NoRoad,155,NoRoad,NoRoad,NoRoad,380,438,NoRoad,41,266,NoRoad,NoRoad,104,867,609,NoRoad,270,861,NoRoad,NoRoad,165,NoRoad,675,250,686,995,366,191,NoRoad,433,NoRoad,

870,940,81,893,NoRoad,926,134,385,NoRoad,565,519,NoRoad,NoRoad,313,851,NoRoad,NoRoad,NoRoad,248,220,NoRoad,826,359,829,NoRoad,234,198,145,409,68,359,NoRoad,814,218,186,NoRoad,NoRoad,929,203,NoRoad,

NoRoad,143,NoRoad,854,677,NoRoad,219,652,663,36,NoRoad,NoRoad,313,NoRoad,132,NoRoad,433,598,NoRoad,NoRoad,168,870,NoRoad,NoRoad,NoRoad,128,437,NoRoad,383,364,966,227,NoRoad,NoRoad,807,993,NoRoad,NoRoad,526,17,

869,NoRoad,402,718,NoRoad,NoRoad,457,160,236,767,NoRoad,380,851,132,NoRoad,NoRoad,596,903,613,730,NoRoad,261,NoRoad,142,379,885,89,NoRoad,848,258,112,NoRoad,900,NoRoad,NoRoad,818,639,268,600,NoRoad,

624,162,813,427,727,582,639,NoRoad,36,684,256,438,NoRoad,NoRoad,NoRoad,NoRoad,539,379,664,561,542,NoRoad,999,585,NoRoad,NoRoad,321,398,NoRoad,NoRoad,950,68,193,NoRoad,697,NoRoad,390,588,848,NoRoad,

300,122,649,448,793,NoRoad,538,890,403,344,990,NoRoad,NoRoad,433,596,539,NoRoad,NoRoad,73,NoRoad,318,NoRoad,NoRoad,500,NoRoad,968,NoRoad,291,NoRoad,NoRoad,765,196,504,757,NoRoad,542,NoRoad,395,227,148,

609,910,386,916,457,861,374,812,286,489,801,41,NoRoad,598,903,379,NoRoad,NoRoad,NoRoad,946,136,399,NoRoad,941,707,156,757,258,251,NoRoad,807,NoRoad,NoRoad,NoRoad,461,501,NoRoad,NoRoad,616,NoRoad,

131,NoRoad,252,258,981,NoRoad,NoRoad,605,986,565,154,266,248,NoRoad,613,664,73,NoRoad,NoRoad,686,NoRoad,NoRoad,575,627,817,282,NoRoad,698,398,222,NoRoad,649,NoRoad,NoRoad,NoRoad,NoRoad,NoRoad,654,NoRoad,NoRoad,

NoRoad,729,391,NoRoad,191,683,NoRoad,953,NoRoad,NoRoad,53,NoRoad,220,NoRoad,730,561,NoRoad,946,686,NoRoad,NoRoad,389,729,553,304,703,455,857,260,NoRoad,991,182,351,477,867,NoRoad,NoRoad,889,217,853,

251,802,264,760,NoRoad,293,NoRoad,NoRoad,NoRoad,NoRoad,474,NoRoad,NoRoad,168,NoRoad,542,318,136,NoRoad,NoRoad,NoRoad,NoRoad,392,NoRoad,NoRoad,NoRoad,267,407,27,651,80,927,NoRoad,974,977,NoRoad,NoRoad,457,117,NoRoad,

NoRoad,941,637,909,NoRoad,NoRoad,966,NoRoad,810,103,650,104,826,870,261,NoRoad,NoRoad,399,NoRoad,389,NoRoad,NoRoad,NoRoad,202,NoRoad,NoRoad,NoRoad,NoRoad,867,140,403,962,785,NoRoad,511,NoRoad,1,NoRoad,707,NoRoad,

NoRoad,922,349,529,NoRoad,NoRoad,NoRoad,NoRoad,761,810,402,867,359,NoRoad,NoRoad,999,NoRoad,NoRoad,575,729,392,NoRoad,NoRoad,388,939,NoRoad,959,NoRoad,83,463,361,NoRoad,NoRoad,512,931,NoRoad,224,690,369,NoRoad,

NoRoad,573,NoRoad,311,351,66,NoRoad,79,574,463,NoRoad,609,829,NoRoad,142,585,500,941,627,553,NoRoad,202,388,NoRoad,164,829,NoRoad,620,523,639,936,NoRoad,NoRoad,490,NoRoad,695,NoRoad,505,109,NoRoad,

856,531,NoRoad,404,969,NoRoad,NoRoad,NoRoad,53,733,NoRoad,NoRoad,NoRoad,NoRoad,379,NoRoad,NoRoad,707,817,304,NoRoad,NoRoad,939,164,NoRoad,NoRoad,616,716,728,NoRoad,889,349,NoRoad,963,150,447,NoRoad,292,586,264,

221,539,NoRoad,NoRoad,925,27,NoRoad,712,793,665,NoRoad,270,234,128,885,NoRoad,968,156,282,703,NoRoad,NoRoad,NoRoad,829,NoRoad,NoRoad,NoRoad,822,NoRoad,NoRoad,NoRoad,736,576,NoRoad,697,946,443,NoRoad,205,194,

514,667,108,NoRoad,987,NoRoad,NoRoad,613,NoRoad,494,966,861,198,437,89,321,NoRoad,757,NoRoad,455,267,NoRoad,959,NoRoad,616,NoRoad,NoRoad,NoRoad,349,156,339,NoRoad,102,790,359,NoRoad,439,938,809,260,

NoRoad,607,NoRoad,588,328,NoRoad,449,312,NoRoad,644,NoRoad,NoRoad,145,NoRoad,NoRoad,398,291,258,698,857,407,NoRoad,NoRoad,620,716,822,NoRoad,NoRoad,293,486,943,NoRoad,779,NoRoad,6,880,116,775,NoRoad,947,

591,NoRoad,727,680,282,290,120,452,777,863,NoRoad,NoRoad,409,383,848,NoRoad,NoRoad,251,398,260,27,867,83,523,728,NoRoad,349,293,NoRoad,212,684,505,341,384,9,992,507,48,NoRoad,NoRoad,

762,920,225,875,589,NoRoad,797,NoRoad,330,25,406,165,68,364,258,NoRoad,NoRoad,NoRoad,222,NoRoad,651,140,463,639,NoRoad,NoRoad,156,486,212,NoRoad,NoRoad,349,723,NoRoad,NoRoad,186,NoRoad,36,240,752,

182,NoRoad,578,NoRoad,NoRoad,786,358,978,936,385,989,NoRoad,359,966,112,950,765,807,NoRoad,991,80,403,361,936,889,NoRoad,339,943,684,NoRoad,NoRoad,965,302,676,725,NoRoad,327,134,NoRoad,147,

56,NoRoad,699,615,873,NoRoad,232,900,883,NoRoad,772,675,NoRoad,227,NoRoad,68,196,NoRoad,649,182,927,962,NoRoad,NoRoad,349,736,NoRoad,NoRoad,505,349,965,NoRoad,474,178,833,NoRoad,NoRoad,555,853,NoRoad,

NoRoad,315,NoRoad,NoRoad,477,554,550,NoRoad,286,342,932,250,814,NoRoad,900,193,504,NoRoad,NoRoad,351,NoRoad,785,NoRoad,NoRoad,NoRoad,576,102,779,341,723,302,474,NoRoad,689,NoRoad,NoRoad,NoRoad,451,NoRoad,NoRoad,

884,649,898,409,NoRoad,817,NoRoad,901,NoRoad,470,7,686,218,NoRoad,NoRoad,NoRoad,757,NoRoad,NoRoad,477,974,NoRoad,512,490,963,NoRoad,790,NoRoad,384,NoRoad,676,178,689,NoRoad,245,596,445,NoRoad,NoRoad,343,

412,937,294,758,NoRoad,33,305,NoRoad,174,NoRoad,NoRoad,995,186,807,NoRoad,697,NoRoad,461,NoRoad,867,977,511,931,NoRoad,150,697,359,6,9,NoRoad,725,833,NoRoad,245,NoRoad,949,NoRoad,270,NoRoad,112,

273,NoRoad,NoRoad,221,19,NoRoad,997,NoRoad,NoRoad,NoRoad,823,366,NoRoad,993,818,NoRoad,542,501,NoRoad,NoRoad,NoRoad,NoRoad,NoRoad,695,447,946,NoRoad,880,992,186,NoRoad,NoRoad,NoRoad,596,949,NoRoad,91,NoRoad,768,273,

636,185,575,NoRoad,450,54,662,225,NoRoad,NoRoad,391,191,NoRoad,NoRoad,639,390,NoRoad,NoRoad,NoRoad,NoRoad,NoRoad,1,224,NoRoad,NoRoad,443,439,116,507,NoRoad,327,NoRoad,NoRoad,445,NoRoad,91,NoRoad,248,NoRoad,344,

NoRoad,102,168,NoRoad,NoRoad,506,744,533,NoRoad,730,NoRoad,NoRoad,929,NoRoad,268,588,395,NoRoad,654,889,457,NoRoad,690,505,292,NoRoad,938,775,48,36,134,555,451,NoRoad,270,NoRoad,248,NoRoad,371,680,

NoRoad,636,432,76,NoRoad,386,686,770,828,582,NoRoad,433,203,526,600,848,227,616,NoRoad,217,117,707,369,109,586,205,809,NoRoad,NoRoad,240,NoRoad,853,NoRoad,NoRoad,NoRoad,768,NoRoad,371,NoRoad,540,

774,289,833,257,NoRoad,381,239,722,711,468,933,NoRoad,NoRoad,17,NoRoad,NoRoad,148,NoRoad,NoRoad,853,NoRoad,NoRoad,NoRoad,NoRoad,264,194,260,947,NoRoad,752,147,NoRoad,NoRoad,343,112,273,344,680,540,NoRoad

};

// Define a class for Edges

class Edge {

public:

int enda;

int endb;

int length;

bool inMST; Edge( int a, int b, int dist): enda(a), endb(b), length(dist), inMST(false) {}

void setInUse() { inMST =true;}

};

typedef unordered_set Copse;

typedef unordered_set Forest;

struct EdgeSortFunc {

inline bool operator() ( Edge &a , Edge &b)

{ return a.length < b.length;}

};

Copse * findInForest(Forest &f, int bush)

{

for (auto fit=f.begin(); fit!= f.end() ; fit++)

{

if ( (*fit)->count(bush) > 0 )

{

return *fit;

}

}

return nullptr;

}

typedef vector< Edge > EdgeList;

int main()

{

// Step 1, Build and sort the Edge List , check the asdM for errors whilst we are doing this.

EdgeList edges= EdgeList();

for (int r=0; r < nBushes; r++)

{

if (adjM[r][r] != NoRoad) return -1;

for (int c= r+1; c< nBushes ; c++)

{

if ( adjM[c][r] != adjM[r][c]) return -2;

if ( adjM[c][r] == NoRoad) continue;

edges.push_back(Edge(c,r, adjM[c][r]));

}

}

sort(edges.begin(), edges.end(), EdgeSortFunc());

// Make the empty forest

Forest forest= Forest();

`int beforeTotal=0;`

for (auto i= edges.begin(); i!= edges.end(); i++) beforeTotal += i->length;

// Eaxamin each edge in turn.

for (auto it=edges.begin(); it != edges.end(); it++ )

{

Copse *a=NULL, *b=NULL;

a = findInForest(forest, it->enda);

b = findInForest(forest, it->endb);

// There are four cases cases, case 4 splits into trivial (forget the the road) or non-trivial join copses

if (a==NULL && b== NULL)

{

// add both a and b form a new copse

Copse* c = new Copse();

c->insert(it->enda);

c->insert(it->endb);

// add new copse to forest

forest.insert(c);

// set road as in the MST

it->setInUse();

}

else if (b==NULL) // thus A must be non null

{

// b to copse a which contains bush 'enda'

a->insert(it->endb);

it->setInUse();

}

else if (a==NULL) // B must be non null etc. mirror of above

{

b->insert(it->enda);

it->setInUse();

}

else // Both ends in a copse

{

// first the easy case where the road is redundant

if (a==b) continue;

// otherwise Transfer the nodes from copse B to the copse A

for ( auto nit= b->begin(); nit != b->end(); nit++)

a->insert(*nit);

// remove copse b as all its bushes are now in copse A

forest.erase(b);

delete b;

it->setInUse();

}

}

// now get the after total

int afterTotal=0;

for (auto i= edges.begin(); i!= edges.end(); i++) if (i->inMST) afterTotal += i->length;

cout << beforeTotal-afterTotal << endl;

`return 0;`

}