Esempio n. 1
0
// 1 color a 3 node, 2 edge graph
// the node with the most edges should be spilled
void test_chaitin_spill() {
    DEBUG_PRINT("\ntest_chaitin_spill:\n");
    DEBUG_PRINT("  creating graph...\n");

    graph* g = create_graph();

    vertex* a = graph_add_vertex(g, 'a');
    vertex* b = graph_add_vertex(g, 'b');
    vertex* c = graph_add_vertex(g, 'c');

    graph_add_edge(a, b);
    graph_add_edge(a, c);
    
    assert(a->color == -1);
    assert(b->color == -1);
    assert(c->color == -1);

    DEBUG_PRINT("  coloring graph...\n");

    color_graph(g, 1);

    #ifdef DEBUG
    print_graph(g);
    #endif

    assert(a->color == -1); // spilled node
    assert(b->color == 0);
    assert(c->color == 0);

    destroy_graph(g);

    printf("+ algorithm test (spill) passed!\n");
}
Esempio n. 2
0
int main(int argc,char *argv[]) {


	int num_houses,num_schools;
	int u,v,weight;
	

	if(scanf("%d %d",&num_houses,&num_schools) != 2) {
		printError();
	}

	
	Graph *g = graph_new(num_houses+num_schools);
	g->num_houses = num_houses;
	g->num_schools = num_schools;
	g->number_of_vertices = num_houses+num_schools;

	while(scanf("%d %d %d",&u,&v,&weight) == 3) {

		if(u < 0 || v < 0 
		|| u>=(g->number_of_vertices) || v>=(g->number_of_vertices) || weight < 0){
			printError();
		}
		else {
			// the 0 represents unvisted 
			graph_add_edge(g,u,v,0);
			graph_add_edge(g,v,u,0);
			//adding weights 
			g->vertices[u].edges[g->vertices[u].num_edges-1].weight = weight;
			g->vertices[v].edges[g->vertices[v].num_edges-1].weight = weight;
		}

	}
	// checking if graph is connected 
	if(!graph_check_connected(g,0)) {
		fprintf(stderr, "Graph is not connected\n");
		exit(EXIT_FAILURE);
	}

	
	Universe *Uni = create_all_sets(g->num_schools,g->num_houses);

	//running Dijkstra on each school vertex
	for(int i=num_houses;i<g->number_of_vertices;i++) {

		Dijkstra(g,i,Uni,min_func);

	}

	setCover(Uni,g->num_houses);



	free_graph(g);
	
	return 0;
}
Esempio n. 3
0
void test_graph_search_create() {
  GRAPH* graph = graph_create();
  graph_add_edge(graph, 0, 1);
  graph_add_edge(graph, 0, 2);
  graph_add_edge(graph, 1, 2);
  graph_add_edge(graph, 2, 1);

  GRAPH_SEARCH* graph_search = graph_search_create(graph);
  graph_destroy(graph);
}
Esempio n. 4
0
static void connect_cg(cgraph cg, struct endp from, struct endp to)
{
  gnode gfrom = endpoint_lookup(cg, &from), gto = endpoint_lookup(cg, &to);

  graph_add_edge(gfrom, gto, NULL);
  /* If an endpoint has args, we must also connect the node w/o args */
  if (from.args_node)
    graph_add_edge(fn_lookup(cg, from.function), gfrom, NULL);
  if (to.args_node)
    graph_add_edge(gto, fn_lookup(cg, to.function), NULL);
}
Esempio n. 5
0
void graph_get_circle(graph *g, int vertices)
{
    graph_init(g, vertices);

    graph_add_edge(g, vertices - 1, vertices - 1, 1.0);
    graph_add_sym_edges(g, 0, vertices - 1, 1.0);

    for (int u = 0; u < vertices - 1; ++u)
    {
        graph_add_edge(g, u, u, 1.0);
        graph_add_sym_edges(g, u, u + 1, 1.0);
    }
}
Esempio n. 6
0
struct GRAPH *graph_prim(struct GRAPH *g) {
	struct EDGE *e;
	struct GRAPH *tree=NULL;
	struct EDGE_LIST *el=NULL, **tmp, **insert, *ne;
	struct VERTEX *v1, *v2;
	int a;
	if(!g)
		return;
	graph_add_vertex(&tree, g->vertex->point);
	for(; g; g=g->next)
		for(e=g->vertex->neighbour; e; e=e->next) {
			for(tmp=&el; *tmp; tmp=&((*tmp)->next))
				if((*tmp)->weight>e->weight)
					break;
			ne=malloc(sizeof(struct EDGE_LIST));
			ne->next=*tmp;
			ne->weight=e->weight;
			ne->v1=g->vertex;
			ne->v2=e->vertex;
			*tmp=ne;
		}
	do {
		for(tmp=&el; *tmp; tmp=&(*tmp)->next) {
			ne=*tmp;
			for(g=tree, a=0; g; g=g->next) {
				if(g->vertex->point.x==ne->v1->point.x&&g->vertex->point.y==ne->v1->point.y) {
					v1=g->vertex;
					a++;
				}
				if(g->vertex->point.x==ne->v2->point.x&&g->vertex->point.y==ne->v2->point.y) {
					v2=g->vertex;
					a+=2;
				}
			}
			if(!a)
				continue;
			else if(a==1)
				graph_add_edge(v1, graph_add_vertex(&tree, ne->v2->point), ne->weight, DIRECTION_SINGLE);
			else if(a==2)
				graph_add_edge(v2, graph_add_vertex(&tree, ne->v1->point), ne->weight, DIRECTION_SINGLE);
			
			*tmp=ne->next;
			free(ne);
			break;
		}
	} while(el);
	return tree;
}
Esempio n. 7
0
static uint8_t _copy_edges(
  graph_t *gin, graph_t *gout, uint32_t *nodemap, uint32_t nnodes) {

  uint64_t  nidx;
  uint64_t  nbridx;
  uint64_t  nnbrs;
  uint32_t *nbrs;

  for (nidx = 0; nidx < nnodes; nidx++) {

    nnbrs = graph_num_neighbours(gin, nidx);
    nbrs  = graph_get_neighbours(gin, nidx);

    for (nbridx = 0; nbridx < nnbrs; nbridx++) {

      if (nodemap[nidx] == nodemap[nbrs[nbridx]]) continue;

      if (graph_add_edge(gout, nodemap[nidx], nodemap[nbrs[nbridx]], 1))
        goto fail;
    }
  }

  return 0;

fail:
  return 1;
}
Esempio n. 8
0
void graph_get_llrgg(graph *g, int vertices, double r, double *x, double *y, well1024 *rng)
{
    graph_init(g, vertices);

    for (int i = 0; i < vertices; ++i)
    {
        x[i] = well1024_next_double(rng);
        y[i] = well1024_next_double(rng);
    }
    double d;
    for (int i = 0; i < vertices; ++i)
    {
        for (int j = 0; j < vertices; ++j)
        {
            if (i != j)
            {
                const double a = x[i] - x[j];
                const double b = y[i] - y[j];
                d = hypot(a, b);
                if (d < r)
                {
                    graph_add_edge(g, i, j, r - d);
                }
            }
        }
    }
}
Esempio n. 9
0
    void create_edges(
            const pgrouting::DirectedGraph& digraph) {
        V_i vertexIt, vertexEnd;
        EO_i e_outIt, e_outEnd;
        EI_i e_inIt, e_inEnd;


        /* for (each vertex v in original graph) */
        for (boost::tie(vertexIt, vertexEnd) = boost::vertices(digraph.graph);
                vertexIt != vertexEnd; vertexIt++) {
            auto vertex = *vertexIt;

           /* for( all incoming edges in to vertex v) */
            for (boost::tie(e_outIt, e_outEnd) =
                    boost::out_edges(vertex, digraph.graph);
                    e_outIt != e_outEnd;
                    e_outIt++) {
                /* for( all outgoing edges out from vertex v) */
                for (boost::tie(e_inIt, e_inEnd) =
                        boost::in_edges(vertex, digraph.graph);
                        e_inIt != e_inEnd; e_inIt++) {
                    /*
                       Prevent self-edges from being created in the Line Graph
                       */

                    graph_add_edge(
                            (digraph.graph[*e_inIt]).id,
                            (digraph.graph[*e_outIt]).id);
                }
            }
        }
    }
Esempio n. 10
0
void graph_get_rec_rgg(graph *g, int vertices, double width, double r, double *x, double *y, well1024 *rng)
{
    graph_init(g, vertices);

    const double length = 1.0 / width; // A = l * w so l = 1 / w

    for (int i = 0; i < vertices; ++i)
    {
        x[i] = well1024_next_double(rng) * length;
        y[i] = well1024_next_double(rng) * width;
    }
    double d;
    for (int i = 0; i < vertices; ++i)
    {
        for (int j = 0; j < vertices; ++j)
        {
            const double a = x[i] - x[j];
            const double b = y[i] - y[j];
            d = hypot(a, b);
            if (d < r)
            {
                graph_add_edge(g, i, j, r - d);
            }
        }
    }
}
Esempio n. 11
0
static int bmz8_gen_edges(cmph_config_t *mph)
{
	cmph_uint8 e;
	bmz8_config_data_t *bmz8 = (bmz8_config_data_t *)mph->data;
	cmph_uint8 multiple_edges = 0;
	DEBUGP("Generating edges for %u vertices\n", bmz8->n);
	graph_clear_edges(bmz8->graph);
	mph->key_source->rewind(mph->key_source->data);
	for (e = 0; e < mph->key_source->nkeys; ++e)
	{
		cmph_uint8 h1, h2;
		cmph_uint32 keylen;
		char *key = NULL;
		mph->key_source->read(mph->key_source->data, &key, &keylen);

//		if (key == NULL)fprintf(stderr, "key = %s -- read BMZ\n", key);
		h1 = (cmph_uint8)(hash(bmz8->hashes[0], key, keylen) % bmz8->n);
		h2 = (cmph_uint8)(hash(bmz8->hashes[1], key, keylen) % bmz8->n);
		if (h1 == h2) if (++h2 >= bmz8->n) h2 = 0;
		if (h1 == h2)
		{
			if (mph->verbosity) fprintf(stderr, "Self loop for key %u\n", e);
			mph->key_source->dispose(mph->key_source->data, key, keylen);
			return 0;
		}
		//DEBUGP("Adding edge: %u -> %u for key %s\n", h1, h2, key);
		mph->key_source->dispose(mph->key_source->data, key, keylen);
//		fprintf(stderr, "key = %s -- dispose BMZ\n", key);
		multiple_edges = graph_contains_edge(bmz8->graph, h1, h2);
		if (mph->verbosity && multiple_edges) fprintf(stderr, "A non simple graph was generated\n");
		if (multiple_edges) return 0; // checking multiple edge restriction.
		graph_add_edge(bmz8->graph, h1, h2);
	}
	return !multiple_edges;
}
Esempio n. 12
0
static void
read_case_file (Graph *graph, 
		const char *filename)
{
     /* open file for reading */
     FILE *file = fopen (filename, "r");
     
     size_t bufsize = 1024;
     char buf[bufsize];

     while(fgets (buf, bufsize, file) != NULL){
	  char *ptr1 = &buf[0];
	  char *machine,*c1,*c2,*price;
	  machine = nextnum (&ptr1);
	  c1 = nextnum (&ptr1);
	  c2 = nextnum (&ptr1);
	  price = nextnum (&ptr1);

	  GraphNode *node1 = graph_lookup_node (graph, c1);
	  GraphNode *node2 = graph_lookup_node (graph, c2); 

	  unsigned int price_val = (unsigned int)strtoul (price, NULL, 10);
	  unsigned int machine_num = (unsigned int)strtoul (machine, NULL, 10);
	  graph_add_edge (node1, node2, machine_num, price_val);
     }

     fclose (file);
}
Esempio n. 13
0
static int chm_gen_edges(cmph_config_t *mph)
{
    cmph_uint32 e;
    chm_config_data_t *chm = (chm_config_data_t *)mph->data;
    int cycles = 0;

    DEBUGP("Generating edges for %u vertices with hash functions %s and %s\n", chm->n, cmph_hash_names[chm->hashfuncs[0]], cmph_hash_names[chm->hashfuncs[1]]);
    graph_clear_edges(chm->graph);
    mph->key_source->rewind(mph->key_source->data);
    for (e = 0; e < mph->key_source->nkeys; ++e)
    {
        cmph_uint32 h1, h2;
        cmph_uint32 keylen;
        char *key;
        mph->key_source->read(mph->key_source->data, &key, &keylen);
        h1 = hash(chm->hashes[0], key, keylen) % chm->n;
        h2 = hash(chm->hashes[1], key, keylen) % chm->n;
        if (h1 == h2) if (++h2 >= chm->n) h2 = 0;
        if (h1 == h2)
        {
            if (mph->verbosity) fprintf(stderr, "Self loop for key %u\n", e);
            mph->key_source->dispose(mph->key_source->data, key, keylen);
            return 0;
        }
        DEBUGP("Adding edge: %u -> %u for key %s\n", h1, h2, key);
        mph->key_source->dispose(mph->key_source->data, key, keylen);
        graph_add_edge(chm->graph, h1, h2);
    }
    cycles = graph_is_cyclic(chm->graph);
    if (mph->verbosity && cycles) fprintf(stderr, "Cyclic graph generated\n");
    DEBUGP("Looking for cycles: %u\n", cycles);

    return ! cycles;
}
Esempio n. 14
0
void graph_add_edges(graph *g) {
    node *n1, *n2;
    int i, j;

    for (i = 0; i < g->ntemplates; i++) {
	n1 = g->nodes->node[i];
	for (j = i+1; j < g->ntemplates; j++) {
	    int count;
	    double score;
	    n2 = g->nodes->node[j];

	    score = calc_edge_score(n1->matrix, n2->matrix,
				    g->snp_scores, g->nsnps, &count,
				    g->correlation_offset);
	    if (count) {
		graph_add_edge(g, g->nodes->node[i], g->nodes->node[j], score);
	    }
	}
    }

    /* Sort edges-out for each node by node number */
    for (i = 0; i < g->ntemplates; i++) {
	node_sort_edges(g->nodes->node[i]);
    }
}
Esempio n. 15
0
 //! \brief Inserts *count* edges of type *pgr_edge_t* into the graph
 void graph_insert_data(const pgr_edge_t *data_edges, int64_t count) {
     for (unsigned int i = 0; i < count; ++i) {
         graph_add_edge(data_edges[i]);
     }
     adjust_vertices();
     for ( int64_t i = 0; (unsigned int) i < gVertices_map.size(); ++i )
         graph[i].id = gVertices_map.find(i)->second;
 }
Esempio n. 16
0
 void graph_insert_data( const std::vector <pgr_edge_t > &data_edges) {
     for (const auto edge : data_edges) {
         graph_add_edge(edge);
     }
     adjust_vertices();
     for ( int64_t i = 0; (unsigned int) i < gVertices_map.size(); ++i )
         graph[i].id = gVertices_map.find(i)->second;
 }
Esempio n. 17
0
/*
* In this pass, we are fixing the edges from jmp-like instructions and their
* targets
*/
void x8664_graph_1 (struct _graph * graph,
                    uint64_t        address)
{
    struct _graph_it * it;
    ud_t               ud_obj;

    for (it = graph_iterator(graph); it != NULL; it = graph_it_next(it)) {
        struct _list * ins_list = graph_it_data(it);
        struct _ins  * ins = list_first(ins_list);

        ud_init      (&ud_obj);
        ud_set_mode  (&ud_obj, 64);
        ud_set_input_buffer(&ud_obj, ins->bytes, ins->size);
        ud_disassemble(&ud_obj);

        struct ud_operand * operand;
        switch (ud_obj.mnemonic) {
        case UD_Ijmp  :
        case UD_Ijo   :
        case UD_Ijno  :
        case UD_Ijb   :
        case UD_Ijae  :
        case UD_Ijz   :
        case UD_Ijnz  :
        case UD_Ijbe  :
        case UD_Ija   :
        case UD_Ijs   :
        case UD_Ijns  :
        case UD_Ijp   :
        case UD_Ijnp  :
        case UD_Ijl   :
        case UD_Ijge  :
        case UD_Ijle  :
        case UD_Ijg   :
            operand = &(ud_obj.operand[0]);

            if (operand->type != UD_OP_JIMM)
                break;
            
            uint64_t head = graph_it_index(it);
            uint64_t tail = head
                             + ud_insn_len(&ud_obj)
                             + udis86_sign_extend_lval(operand);

            int type = INS_EDGE_JCC_TRUE;
            if (ud_obj.mnemonic == UD_Ijmp)
                type = INS_EDGE_JUMP;

            struct _ins_edge * ins_edge = ins_edge_create(type);
            graph_add_edge(graph, head, tail, ins_edge);
            object_delete(ins_edge);
            break;
        default :
            break;
        }
    }
}
Esempio n. 18
0
struct edge * graph_join(struct graph * g, int v1, int v2) {
  struct vertex *a = graph_make_vertex(g, v1);
  struct vertex *b = graph_make_vertex(g, v2);
  
  if (a != NULL && b != NULL) {
    return graph_add_edge(g, a, b);
  }
  return NULL;
}
Esempio n. 19
0
void graph_get_star(graph *g, int vertices)
{
    graph_init(g, vertices);

    for (int u = 0; u < vertices; ++u)
    {
        graph_add_edge(g, u, u, 1.0);
        graph_add_sym_edges(g, u, 0, 1.0);
    }
}
Esempio n. 20
0
graph_t kruskal(graph_t graph) {
    graph_t result = graph_empty(graph_vertices_count(graph));
    unsigned int L, R, num_edges = graph_edges_count(graph);
    vertex_t l = NULL, r = NULL;
    pqueue_t Q = pqueue_empty(num_edges);
    union_find_t C = union_find_create(graph_vertices_count(graph));
    edge_t E = NULL, *edges = graph_edges(graph);
    for (unsigned int i = 0; i < num_edges; i++) {
        pqueue_enqueue(Q, edges[i]);
    }

    free(edges);
    edges = NULL;

    while (!pqueue_is_empty(Q) && union_find_count(C) > 1) {
        E = edge_copy(pqueue_fst(Q));
        l = edge_left_vertex(E);
        r = edge_right_vertex(E);
        L = union_find_find(C, vertex_label(l));
        R = union_find_find(C, vertex_label(r));

        if (L != R) {
            union_find_union(C, L, R);
            E = edge_set_primary(E, true);
        } else {
            E = edge_set_primary(E, false);
        }
        result = graph_add_edge(result, E);
        pqueue_dequeue(Q);
    }

    while (!pqueue_is_empty(Q)) {
        E = edge_copy(pqueue_fst(Q));
        pqueue_dequeue(Q);
        E = edge_set_primary(E, false);
        result = graph_add_edge(result, E);
    }

    Q = pqueue_destroy(Q);
    C = union_find_destroy(C);

    return result;
}
Esempio n. 21
0
uint8_t _update_avg_graph(
  graph_t      *gin,
  graph_t      *gavg,
  array_t      *nodemap,
  edge_weight_t edgeweight,
  uint16_t      ninputs) {

  uint64_t  i;
  uint64_t  j;
  uint32_t  ginnodes;
  uint32_t *nbrs;
  uint32_t  nnbrs;
  uint32_t  outi;
  uint32_t  outj;
  float     inwt;
  float     outwt;

  ginnodes = graph_num_nodes(gin);

  for (i = 0; i < ginnodes; i++) {

    outi  = *(uint32_t *)array_getd(nodemap, i);
    nnbrs = graph_num_neighbours(gin, i);
    nbrs  = graph_get_neighbours(gin, i);

    for (j = 0; j < nnbrs; j++) {

      if (i >= nbrs[j]) continue;

      outj = *(uint32_t *)array_getd(nodemap, nbrs[j]);

      /*will have no effect if edge already exists*/
      graph_add_edge(gavg, outi, outj, 0.0);

      inwt  = graph_get_weight(gin,  i,    nbrs[j]);
      outwt = graph_get_weight(gavg, outi, outj);

      switch (edgeweight) {

        case SUM_WEIGHTS: outwt = outwt + inwt;           break;
        case COUNT_EDGES: outwt = outwt + 1;              break;
        case AVG_WEIGHTS: outwt = outwt + (inwt/ninputs); break;
        default:          goto fail;
      }

      if (graph_set_weight(gavg, outi, outj, outwt))
         goto fail;
    }
  }

  return 0;

fail:
  return 1;
}
Esempio n. 22
0
/* creates graph structure from a file*/
graph_t *
graph_create_file (const char *filename)
{
    int i, j, c;
    FILE *file;
    graph_t *g;

    if (!filename)
    {
        fprintf (stderr, "file name is null \n");
        _FLINE_;
        exit (0);
    }

    file = fopen (filename, "r");
    if (!file)
    {
        fprintf (stderr, "file %s can not open\n", filename);
        _FLINE_;
        exit (0);
    }

    c = fscanf (file, "%d %d", &i, &j);
    if (!(i > 0 && j > 0))
    {
        fprintf (stderr, "Node number and Edge number should be positive \n");
        _FLINE_;
        exit (0);
    }
    g = graph_create (i, j);
    if (!g)
    {
        fprintf (stderr, "graph creation failed for nodes %2d and edges %2d \n", i, j);
        _FLINE_;
        exit (0);
    }

    c = fscanf (file, "%d %d", &i, &j);
    while (c != EOF)
    {
        if ((i < 0 || j < 0))
        {
            fprintf (stderr, "Nodes should be positive \n");
            _FLINE_;
            exit (0);
        }
        graph_add_edge (g, i, j);
        c = fscanf (file, "%d %d", &i, &j);
    }

    (void)fclose (file);
    return g;
}
Esempio n. 23
0
void graph_get_complete(graph *g, int vertices)
{
    graph_init(g, vertices);

    for (int i = 0; i < vertices; ++i)
    {
        for (int j = 0; j < vertices; ++j)
        {
            graph_add_edge(g, i, j, 1.0);
        }
    }
}
Esempio n. 24
0
void testFINDPATH()
{
    graph_t *g = mkGraphABCDE();
    graph_add_edge(g, "A", "B", NULL);
    graph_add_edge(g, "B", "C", NULL);
    graph_add_edge(g, "C", "D", NULL);
    graph_add_edge(g, "A", "E", NULL);
    list_t *path = graph_find_path(g, "A", "D");

    void *n0, *n1, *n2;
    CU_ASSERT(list_nth(path, 0, &n0) && streq(n0, "B"));
    CU_ASSERT(list_nth(path, 1, &n1) && streq(n1, "C"));
    CU_ASSERT(list_nth(path, 2, &n2) && streq(n2, "D"));
    list_free(path);

    // now we add a shortcut and try again:
    graph_add_edge(g, "A", "D", NULL);
    path = graph_find_path(g, "A", "D");
    CU_ASSERT(list_nth(path, 0, &n0) && streq(n0, "D") && list_len(path) == 1);
    list_free(path);

    // now we add a cycle and try again:
    graph_add_edge(g, "D", "A", NULL);
    path = graph_find_path(g, "A", "D");
    CU_ASSERT(list_nth(path, 0, &n0) && streq(n0, "D") && list_len(path) == 1);
    list_free(path);

    freeGraphABCDE(g);
}
Esempio n. 25
0
int main(void) {
   
    int d = 0;
    int i = 0;
    int x = 0;
    graph my_graph = NULL;


    if (1 == scanf("%d", &d)) {
        my_graph = graph_new(d);
    }
    while (2 == scanf("%d%d", &i, &x)) {
        graph_add_edge(my_graph, i, x);
    }

    /*
    graph my_graph = graph_new(8);

    graph_bi_add_edge(my_graph, 0, 1);
    graph_bi_add_edge(my_graph, 0, 4);
    
    graph_bi_add_edge(my_graph, 1, 0);
    graph_bi_add_edge(my_graph, 1, 5);
    
    graph_bi_add_edge(my_graph, 2, 3);
    graph_bi_add_edge(my_graph, 2, 5);
    graph_bi_add_edge(my_graph, 2, 6);
    
    graph_bi_add_edge(my_graph, 3, 2);
    graph_bi_add_edge(my_graph, 3, 6);
    graph_bi_add_edge(my_graph, 3, 7);
    
    graph_bi_add_edge(my_graph, 4, 0);

    graph_bi_add_edge(my_graph, 5, 1);
    graph_bi_add_edge(my_graph, 5, 2);
    graph_bi_add_edge(my_graph, 5, 6);

    graph_bi_add_edge(my_graph, 6, 2);
    graph_bi_add_edge(my_graph, 6, 3);
    graph_bi_add_edge(my_graph, 6, 5);
    graph_bi_add_edge(my_graph, 6, 7);

    graph_bi_add_edge(my_graph, 7, 3);
    graph_bi_add_edge(my_graph, 7, 6);
    */
    
    graph_dfs(my_graph);
    graph_print(my_graph);
    graph_free(my_graph);
    return EXIT_SUCCESS;
}
Esempio n. 26
0
void testFINDNEIGHBORS()
{
    graph_t *g = mkGraphABCDE();

    graph_add_edge(g, "A", "B", NULL);

    list_t *neighs = graph_find_neighbors(g, "A");

    CU_ASSERT(list_has(neighs, streq, "B"))
    CU_ASSERT(list_len(neighs) == 1);
    list_free(neighs);

    // adding an additional neighbor:
    graph_add_edge(g, "A", "C", NULL);
    neighs = graph_find_neighbors(g, "A");
    CU_ASSERT(list_has(neighs, streq, "B"));
    CU_ASSERT(list_has(neighs, streq, "C"));
    CU_ASSERT(list_len(neighs) == 2);
    list_free(neighs);

    // the graph is directed:
    graph_add_edge(g, "C", "A", NULL);
    neighs = graph_find_neighbors(g, "A");
    CU_ASSERT(list_has(neighs, streq, "B"));
    CU_ASSERT(list_has(neighs, streq, "C"));
    CU_ASSERT(list_len(neighs) == 2);
    list_free(neighs);

    // add an unrelated edge
    graph_add_edge(g, "D", "E", NULL);
    neighs = graph_find_neighbors(g, "A");
    CU_ASSERT(list_has(neighs, streq, "B"));
    CU_ASSERT(list_has(neighs, streq, "C"));
    CU_ASSERT(list_len(neighs) == 2);
    list_free(neighs);

    freeGraphABCDE(g);
}
Esempio n. 27
0
void add_edge(char *s1, char *s2) {
	int i, v1=-1, v2=-1;
	for(i=0; i<ARRAY_SIZE; i++) {
		if(!vertex[i])
			continue;
		if(!strcmp(s2, vertex[i]))
			v1=i;
		if(!strcmp(s1, vertex[i]))
			v2=i;
	}
	if(!(v1==-1||v2==-1)) {
		graph_add_edge(v1, v2, 10, DIRECTION_SINGLE);
	}
}
Esempio n. 28
0
int main()
{
    int t;
    int s;
    for(s=0;s<10;s++) {
	int width = (lrand48()%8)+1;
	graph_t*g = graph_new(width*width);
	for(t=0;t<width*width;t++) {
	    int x = t%width;
	    int y = t/width;
	    int w = 1;
#define R (lrand48()%32)
	    if(x>0) graph_add_edge(&g->nodes[t], &g->nodes[t-1], R, R);
	    if(x<width-1) graph_add_edge(&g->nodes[t], &g->nodes[t+1], R, R);
	    if(y>0) graph_add_edge(&g->nodes[t], &g->nodes[t-width], R, R);
	    if(y<width-1) graph_add_edge(&g->nodes[t], &g->nodes[t+width], R, R);
	}
	
	int x = graph_maxflow(g, &g->nodes[0], &g->nodes[width*width-1]);
	printf("max flow: %d\n", x);
	graph_delete(g);
    }
}
Esempio n. 29
0
int main(int argc, char **argv) {
	int i, j;
	struct GRAPH *g, *tree=NULL;
	struct VERTEX *v;
	struct POINT p;
	DARNIT_KEYS keys;
	srand(time(NULL));
	d_init("Graphs", "uppg8", NULL);
	
	for(i=0; i<256; i++) {
		circle[i]=d_render_circle_new(32, 1);
		line[i]=d_render_line_new(1, 1);
	}
	
	for(j=0; j<4; j++)
		for(i=0; i<7; i++) {
			p.x=i*100+50;//(rand()%80)*10;
			p.y=j*100+50;//(rand()%48)*10;
			v=graph_add_vertex(&graph, p);
			for(g=graph; g; g=g->next)
				if(!(rand()%6))
					graph_add_edge(v, g->vertex, rand()%100, DIRECTION_DOUBLE);
		}
	tree=graph_prim(graph);
	draw_graph(graph);
	for(g=graph;;) {
		keys=d_keys_get();
		if(keys.start) {
			g=g==graph?tree:graph;
			draw_graph(g);
		}
		d_keys_set(keys);
		d_render_begin();
		d_render_tint(0xFF, 0xFF, 0xFF, 0xFF);
		for(i=0; i<vertices; i++)
			d_render_circle_draw(circle[i]);
		for(i=0; i<edges; i++) {
			d_render_tint(length[i], 0x7F, 0x0, 0xFF);
			d_render_line_draw(line[i], 1);
		}
		
		d_render_end();
		d_loop();
	}
	
	d_quit();
	return 0;
}
Esempio n. 30
0
graph_t *graph_plant_path(index_t n, index_t idx)
{
    graph_t *g = graph_alloc(n);
    index_t last = -1;
    index_t t[128];
    index_t k = 0;
    for(index_t i = 0; i < n; i++) {
        if((idx >> i)&1) {
            if(last >= 0)
                graph_add_edge(g, last, i);
            last = i;
            t[k++] = i;
            graph_set_color(g, i, 1);
        } else {
            graph_set_color(g, i, 0);
        }
    }