Exemple #1
0
GraphNode*
graph_lookup_node (Graph *graph,
		   const char *name)
{
     ENTRY e,*ep;
     e.key = (char*)name;
     GraphNode  *node = NULL;
     hsearch_r (e, FIND, &ep, &graph->htab);
     if (ep != NULL)
	  node = ep->data;

     if (node == NULL) {

	  /* create and insert node */
	  node = malloc (sizeof (GraphNode));
	  node->name = (unsigned int)strtoul (name, NULL, 10);
	  node->num_edges = 0;
	  node->edges = malloc ( sizeof (GraphEdge*) *MIN_INITIAL_SIZE);	

	  ENTRY nentry = {strdup (name), node};	  
	  hsearch_r (nentry, ENTER, &ep, &graph->htab);
	  
	  /* add to nodes array */
	  graph_insert_node (graph, node);
     }

     return node;
}
Exemple #2
0
rdp_tree_node_data* rdp_add_node(char* id, rdp_tree_node_data* rdp_tree)
{
  if (rdp_tree_update)
  {
     rdp_tree_node_data *node  = (rdp_tree_node_data*) graph_insert_node(sizeof(rdp_tree_node_data), rdp_tree);
     if (id != NULL)
       node->id = id;
     else
       memcpy(node, text_scan_data, sizeof(scan_data));
       return node;
  }
  else
    return NULL;
}
Exemple #3
0
rdp_tree_node_data* rdp_add_child(char* id, rdp_tree_node_data* rdp_tree)
{
  if (rdp_tree_update)
  {
    rdp_tree_last_child = (rdp_tree_node_data*) graph_insert_node(sizeof(rdp_tree_node_data), rdp_tree);
      if (id != NULL)
        rdp_tree_last_child->id = id;
    else
      memcpy(rdp_tree_last_child, text_scan_data, sizeof(scan_data));

    ((rdp_tree_edge_data*) graph_insert_edge_after_final(sizeof(rdp_tree_edge_data), rdp_tree_last_child, rdp_tree))->rdp_edge_kind = 1;
    return rdp_tree_last_child;
  }
  else
    return NULL;
}
Exemple #4
0
int main(int argc, char **argv)
{
	Graph graph;
	Item item;
	FILE *fp;
	int i, n, w, scelta;
	char citta1[MAX_STR], citta2[MAX_STR];
	int inter, intra;
	
	if(argc < 2){
		fprintf(stderr, "Errore nei parametri!\nUtilizzo: %s <input_file>\n", argv[0]);
		return -1;
	}

	if((fp = fopen(argv[1],"r")) == NULL){
		fprintf(stderr, "Impossibile aprire il file %s\n", argv[1]);
		return -2;
	}

	fscanf(fp, "%d", &n);		//leggo il numero di nodi
	graph = graph_init(n);
	for(i=0; i<n; i++)
	{
		item = item_scan(fp);
		graph_insert_node(graph, item);
	}
	
	//carico archi
	while(fscanf(fp,"%s %s %d", citta1, citta2, &w) == 3)
		graph_connect_nodes(graph, citta1, citta2, w);
	
	fclose(fp);

	printf( "===== M E N U =====\n"
		"1 - Stampa il grado di ogni vertice`\n"
		"2 - Stampa tutti i vertici\n"
		"3 - Stampa i nodi adiacenti ad un nodo\n"
		"4 - Genera la matrice di adiacenza\n"
		"5 - Calcolare flusso inter-regionale e intra-regionale\n"
		"6 - Esci");
	do{
		printf("\nScelta: ");
		scanf("%d", &scelta);
		switch(scelta)
		{
			case 1:
				graph_print_all_degree(graph);
			break;
			case 2:
				graph_print_all_nodes(graph);
			break;
			case 3:
				printf("Introduci la citta` di cui vuoi sapere quali sono i nodi adiacenti: ");
				scanf("%s", citta1);
				graph_print_adj_nodes(graph, citta1);
			break;
			case 4:
				graph_create_adjacency_matrix(graph);
			break;
			case 5:
				graph_intra_inter_flow(graph, &intra, &inter);			
				printf("Flusso intra-regionale: %d\n", intra);	
				printf("Flusso inter-regionale: %d\n", inter);
			break;
		}
	
	}while(scelta != 6);

	graph_destroy(graph);

	return 0;
}