Ejemplo n.º 1
0
int main()
{
    FILE * f = fopen("matrix.txt","r");
    readFromAdjMatrix(f);
    printAdjMatrix();

    //CREATE ADJECENCY LIST FROM MATRIX

    int i;
    struct adjgraph *graph=creategraph(nrOfVerteces);
    for(i=0;i<nrOfVerteces;i++){
        int nrOfNeighbors=getNumberOfNeighborsOfVertex(i);
        int *neighbors=getAllNeighborsOfVertex(i);
        int j;
        for(j=0;j<nrOfNeighbors;j++){
            addedge(graph,i,neighbors[j]);
        }
    }
    printgraph(graph);


    //CREATE ADJACENCY MATRIX FOM LIST
    createadjmatrix(nrOfVerteces);
    for(i=0;i<nrOfVerteces;i++){
        matrix(graph->array[i].head,i);
    }
    return 0;
    bfs(0);
    dfs(0);
    dfsRecurs(0);
    return 0;
}
Ejemplo n.º 2
0
int main (){

	//findroot    (1, 2, 10, &fex,  "fex");  //example from class
	//findroot    (0, 5, 10, &fex2, "fex2");  //example from class

	printgraph(-5, 5, 0.001, &f1a, "f1a");
	findinterval(0.2, 0.3, &f1a, "f1a");
	findinterval(1.2, 1.3, &f1a, "f1a");

	findinterval(1,   2, &f1b, "f1b");
	findinterval(1.71828, 4, &f1b, "f1b");

	findinterval(2,   3,   &f1c, "f1c");
	findinterval(3,   4,   &f1c, "f1c");

	findinterval(4,   5,   &f1d, "f1d");

	//printgraph(0, 1, 0.001, &f3b, "f1a");
	findgraph(0, 1, 0.001, &f3b, "f1a");
	//findroot (0, 1, 10, &f3b, "p3b");

	findgraph(0, 1, 0.001, &fex3, "fex3");
	findgraph(4.4, 4.6, 0.001, &fex4, "fex4");
	findgraph(1, 2, 0.01, &fex5, "fex5");
	
}
graph_t* gengraph(int v, float ep, int seed) {
	graph_t *g;

	srand(seed);
	assert(v > 0);
	assert(ep > 0 && ep < 1);

	g = malloc(sizeof(graph_t));
	memset(g, 0, sizeof(graph_t));

	g->numvert = v;
	g->edgeprob = ep;

	/* Initialize adjacency matrix */
	initgraph(g);
	do {
		/* generate random graph */
		randomize(g);
		/* ensure it is connected */
	} while (!connected(g));

	printgraph(g);

	return g;

}
Ejemplo n.º 4
0
void main()
 {
  char exitint;
  graph maingraph;
  maingraph = getgraph();
  printgraph(maingraph);
  printtopsortorder(maingraph);
  printf("\npress the enter key to quit");
  scanf("%c",&exitint);
 }
Ejemplo n.º 5
0
void printgraph(const ptr &population)
{
	std::cout << "== Printing population" << std::endl;
	int counter = 1;
	for (std::vector<Graph>::const_iterator i = population->begin(); i != population->end(); ++i)
	{
		std::cout << "Graph number " << counter << std::endl;
		printgraph(&(*i));
		++counter;
	}
}
Ejemplo n.º 6
0
int main()
{
    // create the graph given in above fugure
    int V = 5;
    
    struct graph* gra = creatgraph(V);
    addEdge(gra, 0, 1);
    addEdge(gra, 0, 4);
    addEdge(gra, 1, 2);
    addEdge(gra, 1, 3);
    addEdge(gra, 1, 4);
    addEdge(gra, 2, 3);
    addEdge(gra, 3, 4);
 
    // print the adjacency list representation of the above graph
    printgraph(gra);
 
    return 0;
}
Ejemplo n.º 7
0
/* main function */
int main(){
    gnode *adj1[max];
    int n,i,x,y,z,s,t,j,r,des;
    long int e;
    //printf("Enter the no of test cases:\n");
    scanf("%d",&t);
    for(j=0;j<t;j++){
        printf("Enter the no of nodes:\n");
        scanf("%d",&n);
        printf("Enter the no of edges:\n");
        scanf("%d",&e);
        creategraph(adj1,n);
        //srand(time(NULL));
        //printf("Enter the vertices in which edge exists and their corresponding weight:\n");
        for(i=0;i<e;i++){
            x=1+rand()%n;
            y=1+rand()%n;
            z=1+rand()%6;
            r=1+rand()%200;
            if(x!=y){
            inputgraph(adj1,n,x-1,y-1,z,r);
            inputgraph(adj1,n,y-1,x-1,z,r);
            }
            //inputgraph(adj1,n,x-1,y-1,r);
            //inputgraph(adj1,n,y-1,x-1,r);
        }
        printgraph(adj1,n);
        printf("Enter the source vertex:\n");
        scanf("%d",&s);
        printf("Enter the destination vertex:\n");
        scanf("%d",&des);
        dijkshtra(adj1,n,s-1);
        if(d[des-1]==99999)
            printf("-1");
        else
         printf("%d",d[des-1]);
         printf("\n");
    }
    return 0;
}
Ejemplo n.º 8
0
int main()
{
	int v,e;
	printf("\nEnter the no of vertices : ");
	scanf("%d",&v);
	int a[v][v];
	printf("\nEnter no of edges : ");
	scanf("%d",&e);
	int i;
	for(i=0;i<e;i++)
	{
		int src, des;
		printf("\nEnter Source : ");
		scanf("%d",&src);
		printf("\nEnter Destination  : ");
		scanf("%d",&des);
		addEdge(a,src,des);
	}
	createGraph(a,v);
	printf("\n");
	printgraph(a,v);
	return 0;
}
Ejemplo n.º 9
0
int main(int argc, char **argv)
{
    gsl_rng_env_setup();

    T = gsl_rng_default;
    r = gsl_rng_alloc (T);
    gsl_rng_set(r, time(NULL));

    Graph *g = new Graph();
	g->addVertex(Vertex(1, "v1", 0, 0, 0, 1.0, 1.0, 1.0));
	g->addVertex(Vertex(2, "v2", 0, 0, 0, 1.0, 0.2, 0.2));
	g->addVertex(Vertex(3, "v3", 0, 0, 0, 0.2, 1.0, 0.2));
	g->addVertex(Vertex(4, "v4", 0, 0, 0, 0.2, 0.2, 1.0));

	g->addEdge(1, 2);
	g->addEdge(1, 3);
	g->addEdge(3, 4);

	ptr population(new std::vector<Graph>(4, *g));
	/*
	std::cout << "Population" << std::endl;
	printgraph(population);

	ptr reproduced = reproduce(population);
	std::cout << "Reproduced" << std::endl;
	printgraph(reproduced);

	ptr offsprings = genetic(reproduced);
	std::cout << "Offsprings" << std::endl;
	printgraph(offsprings);

	population = succession(population, offsprings);
	std::cout << "After one iteration" << std::endl;
	printgraph(population);
	*/

	int iterationcount = 0;
	double bestv = best(population);
	double bestvold = bestv * 2;

	std::cout << "best " << bestv << "; bestvold " << bestvold << std::endl;
	while (fabs(bestvold - bestv)/bestvold > 0.00000001)
	{
		++iterationcount;
		ptr offsprings = genetic(reproduce(population));
		population = succession(population, offsprings);
		bestvold = bestv;
		bestv = best(population);
	}

	std::cout << "Possible result found after " << iterationcount << "iterations: " << std::endl;
	printgraph(population);

	const Graph *bestgraph = bestg(population);
	for (Graph::vertices_const_iterator i = bestgraph->vertices.begin(); i != bestgraph->vertices.end(); ++i)
	{
		const Vertex &v = i->second;
		std::cout << "\"" << v.name << "\"";
		std::cout << "\t@" << v.x << " " << v.y << " " << v.z;
		std::cout << "\tcolor" << v.r << " " << v.g << " " << v.b;
		std::cout << "\tfrozen light size 1.0" << std::endl;
	}

    gsl_rng_free (r);
	return 0;
}
Ejemplo n.º 10
0
int main() {
	char 	line[BUFSIZ];
	int 	row[BUFSIZ];

	int 	opt = 0;
	FILE*	file= NULL;
	//char*	fn 	= NULL;
	int 	ncol= 0;
	int 	tmp = 0;
	graph*	g 	= NULL;
	int*	ap 	= NULL;	// pointer within g->adjmtx
	router*	r 	= NULL;
	int 	fromto[2] = { 0, 0 };
	for (;;) {
		printf("\n%s\n", menu);
		switch (readNat(&opt)) {
		case 0:
			freegraph(&g);
			goto END;
		case 1:
			printf("%s\n", prompt1);
			//fn = readFilename();
			if ((file = fopen(readFilename(), "r")) == NULL) {
				printf("File not found.\n");
				break;
			}
			// file is opened
			while (getaline(line, BUFSIZ, file)) {//printf("while ...\n");
				tmp = 0;
				if ((tmp = linetoints(line, row)) > 0) {
					if (ncol == 0) {// 1st row
						ncol = tmp;
						if (g != NULL)
							freegraph(&g);	// g == NULL
						g = makegraph(ncol);
						ap = g->adjmtx + intcpy(g->adjmtx, row, ncol);
					} else if (ncol == tmp) {
						ap += intcpy(ap, row, ncol);
					} else {
						printf("File content invalid: different number of elements on different rows.\n");
						break;
					}
				}
			}
			ncol = 0;
			fclose(file);
			//if (isvalidgraph(g)) {// graph is created
				printf("Original routing table is as follows:\n");
				printgraph(g);
			/*} else {
				freegraph(&g);
				printf("File content invalid: elements on upper-left to bottom-right diagonal are not all-zero, ");
				printf("or, the upper-right half of the matrix and the bottom-left half are not symmetric.\n");
			}*/
			
			break;
		case 2:
			if (g == NULL) {
				printf("There is no data currently.\nPlease use option 1 to read a routing table data file first.\n");
				break;
			}
			printf("%s\n", prompt2);
			if (readNat(&tmp) < 0 || tmp > g->size) {
				printf("Not a valid router id.\n");
				break;
			}
			r = makerouter(tmp, g->size);
			buildtable(r, g);
			printf("The routing table for router %d is:\n", r->id);
			printrouter(r);
			freerouter(r);
			break;
		case 3:
			if (g == NULL) {
				printf("There is no data currently.\nPlease use option 1 to read a routing table data file first.\n");
				break;
			}
			printf("%s\n", prompt3);
			if (read2Nats(fromto) < 0 || fromto[0] > g->size || fromto[1] > g->size) {
				printf("Invalid input. Please follow the format:\n\tvalid_src_router_id valid_dest_router_id.\n");
				break;
			}
			// fromto[2] is valid
			r = makerouter(fromto[0], g->size);
			dijkstra(g, r->id, r->varray);
			printpath(r, fromto[1]);
			freerouter(r);
			break;
		default:
			printf("%s\n", prompt0);
			break;
		}
	}
END:
	return 0;
}
Ejemplo n.º 11
0
int main(int argc, char *argv[])
{
    printgraph(7);

    return 0;
}
Ejemplo n.º 12
0
int main(void)
{
	int i=0;
	problem *p = readparameters(&nodes,&edges);
	if(!p) {
		printf("readparameters failed.");
		exit(1);
	}
	printf("Knoten:\t\t%4d\n  Angebot:\t%4d\n  Nachfrage:\t%4d\nKanten:\t\t%4d\n\n",nodes,p->angebot,p->nachfrage,edges);
	readgraph(p);
	printf("Kosten auf Kanten nach dem Einlesen:\n\n");
	printgraph(p);
	matrix *x = newmatrix(p->angebot,p->nachfrage);
	/* Solange noch Spalten oder Zeilen vorhanden, Kante waehlen,
	 * in die Basisloesung aufnehmen und Vogel-Werte neu berechnen. */
/*
	for(i=0; i < p->angebot+p->nachfrage-1; i++)
	{
		waehlekante_vogel(p,x);
		vogel(p);
	}
	printf("\n\nBasislösung nach Vogel:\n\n");
	printmatrix(x);

	p = readparameters(&nodes,&edges);
	readgraph(p);
	x = newmatrix(p->angebot,p->nachfrage);
*/


	for(i=0; i < p->angebot+p->nachfrage-1; i++)
	{
		waehlekante_nwe(p,x);
	}
	printf("\n\nBasislösung nach Nordwest-Ecken-Regel:\n\n");
	printmatrix(x);
	
/*
	p = readparameters(&nodes,&edges);
	readgraph(p);
	x = newmatrix(p->angebot,p->nachfrage);
	
	for(i=0; i < p->angebot+p->nachfrage-1; i++)
	{
		waehlekante_mkk(p,x);
	}
	printf("\n\nBasislösung nach Methode der kleinsten Kosten:\n\n");
	printmatrix(x);
	*/
	
	/* Stepping stone */
	/*
	stepstone(x,p);
	printf("\n\nBasislösung nach Stepping Stone:\n\n");
	printmatrix(x);
	*/
	
	/* Modi */
	modi(p,x);
	printf("\n\nBasislösung nach Modi:\n\n");
	printmatrix(x);

	return 0;
}
Ejemplo n.º 13
0
/*********************************
* perform upper triangualation   *
*********************************/
void uppertriangulation(graphs * graph, int *gap, int verbose)
{
   int vnodenum, cnodenum;

   int currentvnode, currentleftcnode, currentrightcnode;

   int node, socket, newvnode, newcnode, tonode, tosocket;

   if (verbose)
      printf("upper triangulation ...\n");
   /* initialize gap */
   *gap = 0;

   /* we need to make sure that graph does not */
   /* contain double edges */
   if (verbose > 0)
      printhmatrixtall(graph);
   removedoubleoverlaps(graph, 0);


   vnodenum = (*graph).vnodenum;
   cnodenum = (*graph).cnodenum;

   currentvnode = 0;
   currentleftcnode = 0;
   currentrightcnode = (cnodenum - 1);

   /* use the "value" field temporarily */
   /* to indicate the residual degree */
   for (node = 0; node < vnodenum; node++)
      vnodevalue = (double)vnodedegree;

   while (currentleftcnode <= currentrightcnode)
   {
      if (verbose > 0)
      {
         printgraph(graph, 7);
      }

      /* find next vnode for diagonal extension */
      newvnode = findsmallestdegreevnode(graph, currentvnode);
      if (verbose)
         printf("newvnode:=%d\n", newvnode);

      /* switch positions of newvnode and currenvnode */
      if (verbose)
         printf("switch position of newvnode\n");
      switchvnode(graph, currentvnode, newvnode);
      if (verbose)
         printgraph(graph, 7);

      /* now take action depending on the */
      /* residual degree of this vnode */
      node = currentvnode;
      switch ((int)vnodevalue)
      {
      case 1:
         if (verbose)
         {
            printf("we are in the case of a degree one node\n");
            printf("this node has residual degree %g\n", vnodevalue);
         }

         /* find a cnode between currentleftcnode */
         /* and currentrightcnode */
         newcnode = -1;
         for (socket = 0; socket < vnodedegree; socket++)
         {
            if ((vnodeedge.dest >= currentleftcnode) && (vnodeedge.dest <= currentrightcnode))
            {
               newcnode = vnodeedge.dest;
               break;
            }
         }
         assert(newcnode >= 0);
         if (verbose)
            printf("newcnode:=%d\n", newcnode);

         /* decrease residual degrees for all */
         /* variable nodes connected to this cnode */
         if (verbose)
            printf("decrease degrees of all connected variable nodes\n");
         tonode = newcnode;
         for (tosocket = 0; tosocket < ctonodedegree; tosocket++)
            (*((*graph).vnodelist + (cnodetoedge.dest))).value -= 1;

         /* now switch position of newcnode and update counters */
         if (verbose)
            printf("switch position of newcnode\n");
         switchcnode(graph, currentleftcnode, newcnode);
         currentvnode++;
         currentleftcnode++;
         break;
      default:
         /* make sure residual degree */
         /* is at least two */
         if (vnodevalue <= 1)
            printf("vnodevalue:=%d\n", vnodevalue);
         assert(vnodevalue > 1);

         if (verbose)
         {
            printf("we are in the case of a higher degree node\n");
            printf("this node has residual degree %g\n", vnodevalue);
         }

         /* adjust gap */
         (*gap) += (vnodevalue - 1);

         /* find a cnode between currentleftcnode */
         /* and currentrightcnode */
         newcnode = -1;
         for (socket = 0; socket < vnodedegree; socket++)
         {
            if ((vnodeedge.dest >= currentleftcnode) && (vnodeedge.dest <= currentrightcnode))
            {
               newcnode = vnodeedge.dest;
               break;
            }
         }
         assert(newcnode >= 0);
         if (verbose)
            printf("the chosen cnode is %d\n", newcnode);

         /* decrease residual degrees for all */
         /* variable nodes connected to this cnode */
         if (verbose)
            printf("decrease degrees of all connected variable nodes\n");
         tonode = newcnode;
         for (tosocket = 0; tosocket < ctonodedegree; tosocket++)
            (*((*graph).vnodelist + (cnodetoedge.dest))).value -= 1;

         /* now switch position of newcnode and update counters */
         if (verbose)
            printf("switch position of cnode\n");
         switchcnode(graph, currentleftcnode, newcnode);
         if (verbose)
         {
            printgraph(graph, 7);
         }
         currentvnode++;
         currentleftcnode++;

         /* finally declare all remaining checks which are */
         /* connected to this vnode to be known */
         if (verbose)
            printf("declare all other check nodes to be known\n");

         while (socket < vnodedegree)
         {
            if ((vnodeedge.dest >= currentleftcnode) && (vnodeedge.dest <= currentrightcnode))
            {
               tonode = vnodeedge.dest;
               if (verbose)
                  printf("declare node %d to be known\n", tonode);

               if (verbose)
                  printhmatrixtall(graph);
               /* decrease degree of connected variable nodes */
               if (verbose)
                  printf("decrease degrees\n");
               for (tosocket = 0; tosocket < ctonodedegree; tosocket++)
                  (*((*graph).vnodelist + (cnodetoedge.dest))).value -= 1;

               if (verbose)
                  printf("switch cnodes currentrightcnode %d and tonode %d\n",
                         currentrightcnode, tonode);
               switchcnode(graph, currentrightcnode, tonode);
               if (verbose)
                  printhmatrixtall(graph);
               currentrightcnode--;
            }

            socket++;
         }
         break;
      }
   }

   /* rearrange the matrix to fit paper */
   tonode = cnodenum - *gap - 1;
   for (node = 0; node < (cnodenum - *gap) / 2; node++)
   {
      switchcnode(graph, node, tonode);
      tonode--;
   }
   tonode = vnodenum - 1;
   for (node = 0; node < vnodenum / 2; node++)
   {
      switchvnode(graph, node, tonode);
      tonode--;
   }

   if (verbose)
   {
      printhmatrixtall(graph);
      printhmatrix(graph);
   }
}