Example #1
0
File: tsp.c Project: andlt/MC658
double leftovers_cost(const TSPInstance *instance, const int *partialSolution, int m, tree_node *node)
{
/* Calcula o valor máximo que os vértices faltantes agregarão ao grafo <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<? */
	
	int i = 0, j = 0;
	//node *mst = NULL;
	//int left_n = 0;
	int *used = NULL;
	bool already_in;
	
	used = new int[instance->n];
	for (i = 0; i < instance->n; i++)
	{
		already_in = 0;
		for (j = 0; j < m; j++)
		{
			if (partialSolution[j] == i)
			{
				already_in = 1;
			}
		}
		if (already_in == 0)
		{
			used[i] = 0;
		}
		else
		{
			used[i] = 1;
		}
	}

	//left_n = instance->n - m;

	return mst_prim(instance, partialSolution, 0);
	
	//return sum_mst_pri; //asoidjaoidjs j
}
int main()
{
    int n,i;
    
    // code to take number of vertices
    printf("Enter number of vertceis\n");
    scanf("%d",&n); 
      
    // code to make adjacent list and display 
    printf("\nInstruction:\n\nfirst enter adjacent node and give one space and then enter wieght of edge \nand then do sam for next nodes and if adajcent finish \nthen enter 0 and Press Enter Key\n\n",i); 
    for(i=1;i<=n;i++)
    {
        scan_adj(i);
        //printf("adj complete\n");                
    }
    display_list(n);
    
    //code for spanning tree
    mst_prim(n);
    display_edge(n);
    
    system("pause");
    return 0;
}
/* main function begins */
int main (void)
{
    /* note on graph representation
        -- each test input graph is treated as a unigraph, i.e. undirected
        -- adjacency-matrix representation is used; thus a matrix of {x,y}
            signifies a graph with x vertices, where x == y, with the presence and
            weight of each connecting edge between vertices x and y denoted by the
            value of cell {x,y}
        -- the value of each cell is to be interpreted as follows:
          -- 0: no edge/adjacency
          -- i: edge/adjacency present with weight of integer i
    */

    /* test unigraph 1 */
    int ugraph1_size = 5; /* length/width of matrix */
    int ugraph1[5][5] =   /* matrix itself */
    {
        { 0, 0, 1, 2, 0 },
        { 0, 0, 3, 0, 5 },
        { 2, 3, 0, 6, 4 },
        { 1, 0, 6, 0, 0 },
        { 0, 5, 4, 0, 0 }
    };

    /* edge for matrix, returned by prim; -1 means empty element */
    struct edge ugraph1_mst[ugraph1_size*ugraph1_size];
    for ( int i = 0; i < ( ugraph1_size * ugraph1_size ); i++ )
    {
        ugraph1_mst[i].u = -1;
        ugraph1_mst[i].v = -1;
    }

    /* test unigraph 2 */
    int ugraph2_size = 7; /* length/width of matrix */
    int ugraph2[7][7] =   /* matrix itself */
    {
        { 0, 3, 0, 0, 0, 0, 6 },
        { 3, 0, 0, 4, 0, 0, 8 },
        { 0, 1, 0, 2, 10,0, 0 },
        { 0, 4, 2, 0, 0, 0, 5 },
        { 0, 0, 10,0, 0, 8, 3 },
        { 0, 0, 0, 0, 8, 0, 0 },
        { 6, 8, 0, 5, 3, 0, 0 }
    };
    struct edge ugraph2_mst[ugraph2_size*ugraph2_size];
    for ( int i = 0; i < ( ugraph2_size * ugraph2_size ); i++ )
    {
        ugraph2_mst[i].u = -1;
        ugraph2_mst[i].v = -1;
    }

    /* arbitrary starting vertex */
    int r = 0;

    /* get MST for unigraph1, and print */
    printf ("Matrix:\n");
    ugraph_print ( ugraph1_size, ugraph1 );
    printf ("Minimum spanning tree for matrix, from Prim's algorithm:\n");
    ugraph_mst_print ( ugraph1_size, mst_prim ( ugraph1_size, ugraph1, r, \
                       ugraph1_mst  )  );
    printf ("~~~~~~~~~~~~~~~~~~~~~~~~\n");

    /* get MST for unigraph2, and print */
    printf ("Matrix:\n");
    ugraph_print ( ugraph2_size, ugraph2 );
    printf ("Minimum spanning tree for matrix, from Prim's algorithm:\n");
    ugraph_mst_print ( ugraph2_size, mst_prim ( ugraph2_size, ugraph2, r, \
                       ugraph2_mst  )  );
    printf ("~~~~~~~~~~~~~~~~~~~~~~~~\n");

    /* normal return value */
    return 0;
}