Пример #1
0
void balance(Tree* tree)
{
	int nodes = numberOfNodes(tree);
	//1 eftersom ett träd med två noder alltid kommer vara balanserat.
	if (nodes > 1)
	{
		if (actualDepth(tree->root, 0, 0) > theoreticalDepth(tree))
		{
			int *arr = (int*)calloc(numberOfNodes(tree), sizeof(int));

			moveToArr(tree->root, arr, 0);
			emptyTree(tree, tree->root);
			buildBalanced(tree, arr, nodes);
			free(arr);
			//vill inte skriva ut :(
			//printf("Your tree is now balanced!\n");
		}
		else
		{
			printf("Your tree is already balanced!\n");
		}
	}
	else
	{
		printf("Your tree has to have more than one node!\n");
	}

}
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());
}
unsigned int ParallelCoordinatesGraphProxy::getDataCount() const {
  if (getDataLocation() == NODE) {
    return numberOfNodes();
  } else {
    return numberOfEdges();
  }
}
Пример #5
0
void addNode(Tree* tree, int value)
{
	if (numberOfNodes(tree) == 0)
	{
		tree->root = createNode(value, tree);
	}
	else
	{
		Node* node = tree->root;
		if (search(node, value) == NULL)
		{
			addNode2(node, tree, value);
		}
	}
}
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.
}
Пример #7
0
int BST::numberOfNodes(Node* node) {
    return node == 0 ? 0 : numberOfNodes(node->leftNode) + numberOfNodes(node->rightNode) + 1;
}
Пример #8
0
int PlanRepInc::genusLayout(Layout &drawing) const
{
	Graph testGraph;
	GraphAttributes AG(testGraph, GraphAttributes::nodeGraphics |
		GraphAttributes::edgeGraphics |
		GraphAttributes::nodeStyle |
		GraphAttributes::edgeStyle
		);
	Layout xy;
	NodeArray<node> tcopy(*this, 0);
	EdgeArray<bool> finished(*this, false);
	EdgeArray<edge> eOrig(testGraph, 0);
	Color invalid(0,0,0,0);
	EdgeArray<Color> eCol(*this, invalid);

	if (numberOfNodes() == 0) return 0;

	int nIsolated = 0;
	for(node v : nodes)
		if (v->degree() == 0) ++nIsolated;

	NodeArray<int> component(*this);
	int nCC = connectedComponents(*this,component);

	AdjEntryArray<bool> visited(*this,false);
	int nFaceCycles = 0;

	int colBase = 3;
	int colBase2 = 250;
	for(node v : nodes)
	{
		Color col =Color(colBase,colBase2,colBase);
		colBase = (colBase*3) % 233;
		colBase2 = (colBase*2) % 233;

		if (tcopy[v] == 0)
		{
			node u = testGraph.newNode();
			tcopy[v] = u;
			AG.x(u) = drawing.x(v);
			AG.y(u) = drawing.y(v);
			AG.fillColor(u) = col;
			AG.strokeColor(u) = Color::Red;
			AG.strokeWidth(u) = 8;
		}//if

		for(adjEntry adj1 : v->adjEdges) {
			bool handled = visited[adj1];
			adjEntry adj = adj1;

			do {
				node z = adj->theNode();
				if (tcopy[z] == 0)
				{
					node u1 = testGraph.newNode();
					tcopy[z] = u1;
					AG.x(u1) = drawing.x(z);
					AG.y(u1) = drawing.y(z);
					AG.fillColor(u1) = col;
				}//if not yet inserted in the copy
				if (!finished[adj->theEdge()])
				{
					node w = adj->theEdge()->opposite(z);
					if (tcopy[w] != 0)
					{
						edge e;
						if (w == adj->theEdge()->source())
							e = testGraph.newEdge(tcopy[w], tcopy[z]);
						else e = testGraph.newEdge(tcopy[z], tcopy[w]);
						eOrig[e] = adj->theEdge();
						if (eCol[adj->theEdge()] == invalid)
							AG.strokeColor(e) = eCol[adj->theEdge()];
						else
						{
							eCol[adj->theEdge()] = col;
							AG.strokeColor(e) = col;
						}
						finished[adj->theEdge()] = true;
					}
					/*
					else
					{
						eCol[adj->theEdge()] = col;
					}*/

				}
				visited[adj] = true;
				adj = adj->faceCycleSucc();
			} while (adj != adj1);

			if (handled) continue;

			++nFaceCycles;
		}
	}
	//insert the current embedding order by setting bends
	//for(node v : testGraph.nodes)
	//{
	//	adjEntry ad1 = v->firstAdj();
	//}

	int genus = (numberOfEdges() - numberOfNodes() - nIsolated - nFaceCycles + 2*nCC) / 2;
	//if (genus != 0)
	{
		GraphIO::writeGML(AG, "GenusErrorLayout.gml");
	}
	return genus;
}
Пример #9
0
// no comment
int main(int argc, char * argv[])
{

    int thread_create_status,option = 0;
    
    while ((option = getopt (argc, argv, "svi:o::")) != -1){
        switch (option)
        {
            case 'v':
                verbose = true;
                break;
            case 'i':
                netlist_filename = strdup(optarg);
                break;
            case 's':
                should_solve = true;
                break;
            case 'o':
                dcoutput_filename = strdup(optarg);
                break;
            default:
                print_help(argv[0]);
                break;
        }
    }
    
    if(argc < 2 || netlist_filename == NULL){
        print_help(argv[0]);
    }
    
    printf("[-] Reading file: %s\n",netlist_filename);
    
    // number of lines in the input netlist
    int lines_in_netlist = numOfNetlistLines(netlist_filename);
    
    // numof processor cores available for exploitation
    int numofcpus = 1;//(int)sysconf(_SC_NPROCESSORS_ONLN);
    
    pthread_t parsing_threads[numofcpus];
    struct thread_data parsing_threads__data_array[numofcpus];
    
    if(verbose) printf("\n[-] Netlist has %d lines.\n",lines_in_netlist);
    if(verbose) printf("\n[-] Spawning %d threads... \n",numofcpus);
    
    int chunk_size = lines_in_netlist / numofcpus;
    
    // initialize the  list locks
    if (pthread_rwlock_init(&elements_list_lock,NULL) != 0) {
        fprintf(stderr,"[!] Lock init failed\n");
        exit(-1);
    }
    if (pthread_rwlock_init(&options_list_lock,NULL) != 0) {
        fprintf(stderr,"[!] Lock init failed\n");
        exit(-1);
    }

    
    // start the (s)pwnage
    for (int i=0; i < lines_in_netlist/chunk_size; i++){

        int start_line = 1+i*chunk_size;
        int end_line = chunk_size + i* chunk_size;
        
        printf("[-] Assigning lines %d:%d to thread(%d)\n",start_line,end_line,i);
        
        // prepare thread arguments
        parsing_threads__data_array[i].thread_id = i;
        parsing_threads__data_array[i].start_line = start_line;
        parsing_threads__data_array[i].end_line = end_line;
        
        thread_create_status = pthread_create(&parsing_threads[i], NULL, parse_input_netlist, (void *) &parsing_threads__data_array[i]);
    
    }
    
    // wait for all the threads to complete
    for(int i = 0; i < lines_in_netlist/chunk_size; i++) {
        
        int rc = pthread_join(parsing_threads[i], NULL);
        if(rc < 0) { fprintf(stderr,"[!] Error encountered while trying to join thread with main thread\n"); }
    
    }
    
    if(!didParseGroundNode) printf("\n[WARNING] Netlist file looked legit but no ground node found!\n\n");
    
    replace_L_C();
    print_elements_parsed();
    
    numof_circuit_nodes = numberOfNodes(head);
    numof_indie_voltage_sources = numOfIndependentVoltageSources();
    numof_current_sources = numOfCurrentSources();
    
    if(verbose) printf("\n[-] Circuit has %d nodes without the ground node.\n",numof_circuit_nodes);
    if(verbose) printf("[-] Circuit has %d independant Voltage sources.\n\n",numof_indie_voltage_sources);

    if(should_solve){
        solve();
    }
       
    return 0;
}
Пример #10
0
int theoreticalDepth(Tree* tree)
{
	return (int)(log2(numberOfNodes(tree)) + 1);
}