TEST_F(GraphToolsGTest, testGetCompactedGraphUndirectedWeighted1) {
	Graph G(10,true,false);
	G.removeNode(0);
	G.removeNode(2);
	G.removeNode(4);
	G.removeNode(6);
	G.removeNode(8);
	G.addEdge(1,3,0.2);
	G.addEdge(5,3,2132.351);
	G.addEdge(7,5,3.14);
	G.addEdge(7,9,2.7);
	G.addEdge(1,9,0.12345);

	auto nodeMap = GraphTools::getContinuousNodeIds(G);
	auto Gcompact = GraphTools::getCompactedGraph(G,nodeMap);
	
	EXPECT_EQ(G.totalEdgeWeight(),Gcompact.totalEdgeWeight());
	EXPECT_NE(G.upperNodeIdBound(),Gcompact.upperNodeIdBound());
	EXPECT_EQ(G.numberOfNodes(),Gcompact.numberOfNodes());
	EXPECT_EQ(G.numberOfEdges(),Gcompact.numberOfEdges());
	EXPECT_EQ(G.isDirected(),Gcompact.isDirected());
	EXPECT_EQ(G.isWeighted(),Gcompact.isWeighted());
	// TODOish: find a deeper test to check if the structure of the graphs are the same, 
	// probably compare results of some algorithms or compare each edge with a reference node id map.
}
TEST_F(GraphToolsGTest, testRestoreGraph) {
	Graph G(10,false,true);
	G.removeNode(0);
	G.removeNode(2);
	G.removeNode(4);
	G.removeNode(6);
	G.removeNode(8);
	G.addEdge(1,3);
	G.addEdge(5,3);
	G.addEdge(7,5);
	G.addEdge(7,9);
	G.addEdge(1,9);
	auto nodeMap = GraphTools::getContinuousNodeIds(G);
	auto invertedNodeMap = GraphTools::invertContinuousNodeIds(nodeMap,G);
	std::vector<node> reference = {1,3,5,7,9,10};


	EXPECT_EQ(6,invertedNodeMap.size());
	EXPECT_EQ(reference,invertedNodeMap);

	auto Gcompact = GraphTools::getCompactedGraph(G,nodeMap);
	Graph Goriginal = GraphTools::restoreGraph(invertedNodeMap,Gcompact);

	EXPECT_EQ(Goriginal.totalEdgeWeight(),Gcompact.totalEdgeWeight());
	EXPECT_NE(Goriginal.upperNodeIdBound(),Gcompact.upperNodeIdBound());
	EXPECT_EQ(Goriginal.numberOfNodes(),Gcompact.numberOfNodes());
	EXPECT_EQ(Goriginal.numberOfEdges(),Gcompact.numberOfEdges());
	EXPECT_EQ(Goriginal.isDirected(),Gcompact.isDirected());
	EXPECT_EQ(Goriginal.isWeighted(),Gcompact.isWeighted());
}
Exemple #3
0
edge_id Network::addEdge(vertex_id vid1, vertex_id vid2) {
	if (!containsVertex(vid1)) throw ElementNotFoundException("Vertex " + std::to_string(vid1));
	if (!containsVertex(vid2)) throw ElementNotFoundException("Vertex " + std::to_string(vid2));
	if (out_edges[vid1].count(vid2)>0) throw DuplicateElementException("Edge (" + std::to_string(vid1) + "," + std::to_string(vid2) +  ") already exists");
	//max_edge_id++;
	out_edges[vid1].insert(vid2);
	in_edges[vid2].insert(vid1);
	if (!isDirected() && vid1!=vid2) {
		out_edges[vid2].insert(vid1);
		in_edges[vid1].insert(vid2);
	}
	if (isWeighted()) setNumericEdgeAttribute(vid1,vid2,"weight",MULTIPLENETWORK_DEFAULT_WEIGHT); // this value will be replaced if this method has been called inside addEdge(vertex_id, vertex_id, double)
	num_edges++;
	return edge_id(vid1, vid2, isDirected());
}
TEST_F(GraphToolsGTest, testGetCompactedGraphUndirectedUnweighted1) {
	Graph G(10,false,false);
	G.addEdge(0,1);
	G.addEdge(2,1);
	G.addEdge(0,3);
	G.addEdge(2,4);
	G.addEdge(3,6);
	G.addEdge(4,8);
	G.addEdge(5,9);
	G.addEdge(3,7);
	G.addEdge(5,7);

	auto nodeMap = GraphTools::getContinuousNodeIds(G);
	auto Gcompact = GraphTools::getCompactedGraph(G,nodeMap);
	
	EXPECT_EQ(G.numberOfNodes(),Gcompact.numberOfNodes());
	EXPECT_EQ(G.numberOfEdges(),Gcompact.numberOfEdges());
	EXPECT_EQ(G.isDirected(),Gcompact.isDirected());
	EXPECT_EQ(G.isWeighted(),Gcompact.isWeighted());
	// TODOish: find a deeper test to check if the structure of the graphs are the same, 
	// probably compare results of some algorithms or compare each edge with a reference node id map.
}
Exemple #5
0
edge_id Network::addEdge(vertex_id vid1, vertex_id vid2, double weight) {
	if (!isWeighted()) throw OperationNotSupportedException("Cannot add a weight: network is unweighed");
	addEdge(vid1,vid2);
	setNumericEdgeAttribute(vid1, vid2, "weight", weight);
	return edge_id(vid1, vid2, isDirected());
}
Exemple #6
0
void Network::setEdgeWeight(const std::string& vertex_name1, const std::string& vertex_name2, double weight) {
	if (!isWeighted()) throw OperationNotSupportedException("Network is unweighed");
	setNumericEdgeAttribute(vertex_name1, vertex_name2, "weight", weight);
}
Exemple #7
0
double Network::getEdgeWeight(const std::string& vertex_name1, const std::string& vertex_name2) const {
	if (!isWeighted()) throw OperationNotSupportedException("Network is unweighed");
	return getNumericEdgeAttribute(vertex_name1, vertex_name2, "weight");
}
Exemple #8
0
void Network::setEdgeWeight(vertex_id vid1, vertex_id vid2, double weight) {
	if (!isWeighted()) throw OperationNotSupportedException("Network is unweighed");
	setNumericEdgeAttribute(vid1, vid2, "weight", weight);
}
Exemple #9
0
double Network::getEdgeWeight(vertex_id vid1, vertex_id vid2) const {
	if (!isWeighted()) throw OperationNotSupportedException("Network is unweighed");
	return getNumericEdgeAttribute(vid1, vid2, "weight");
}