Grafi in c
Buonasera scusate per l’orario ma vedo realizzare la mappatura con un grafo che contiene 5 destinazioni e tratte:
Ho provato così ma non mi funziona...sapete dirmi cosa non va?
// A C Program to demonstrate adjacency list // representation of graphs #include <stdio.h> #include <stdlib.h> // A structure to represent an adjacency list node struct AdjListNode { char* dest; struct AdjListNode* next; }; // A structure to represent an adjacency list struct AdjList { struct AdjListNode *head; }; // A structure to represent a graph. A graph // is an array of adjacency lists. // Size of array will be V (number of vertices // in graph) struct Graph { char* V; struct AdjList* array; }; // A utility function to create a new adjacency list node struct AdjListNode* newAdjListNode(char* dest) { struct AdjListNode* newNode = (struct AdjListNode*) malloc(sizeof(struct AdjListNode)); newNode->dest = dest; newNode->next = NULL; return newNode; } // A utility function that creates a graph of V vertices struct Graph* createGraph(char V) { struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph)); graph->V = V; // Create an array of adjacency lists. Size of // array will be V graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList)); // Initialize each adjacency list as empty by // making head as NULL char i; for (i = 0; i < V; ++i) graph->array[i].head = NULL; return graph; } // Adds an edge to an undirected graph void addEdge(struct Graph* graph, char src, char dest) { // Add an edge from src to dest. A new node is // added to the adjacency list of src. The node // is added at the begining struct AdjListNode* newNode = newAdjListNode(dest); newNode->next = graph->array[src].head; graph->array[src].head = newNode; // Since graph is undirected, add an edge from // dest to src also newNode = newAdjListNode(src); newNode->next = graph->array[dest].head; graph->array[dest].head = newNode; } // A utility function to print the adjacency list // representation of graph void printGraph(struct Graph* graph) { char v; for (v = 0; v < graph->V; ++v) { struct AdjListNode* pCrawl = graph->array[v].head; printf("\n Adjacency list of vertex %d\n head ", v); while (pCrawl) { printf("-> %d", pCrawl->dest); pCrawl = pCrawl->next; } printf("\n"); } } // Driver program to test above functions int main() { // create the graph given in above fugure char V = 5; struct Graph* graph = createGraph(V); addEdge(graph, "napoli", "milano"); addEdge(graph, "milano", "roma"); addEdge(graph, "napoli", "londra"); addEdge(graph, "bergamo", "parigi"); addEdge(graph, "firenze", "roma"); addEdge(graph, "milano", "roma"); printGraph(graph); return 0; }
Ho provato così ma non mi funziona...sapete dirmi cosa non va?
Risposte
Mi sembra tu faccia confusione tra valori interi [tt]char[/tt] e puntatori [tt]char *[/tt]. Ma a parte questo, quale nodo dovrebbe corrispondere a [tt]"napoli"[/tt] o [tt]"milano"[/tt]? Quello che fa il tuo codice è prendere l'indirizzo di queste stringhe in memoria e prenderne il byte meno significativo. Ma questo valore è probabilmente maggiore di 5 e molto probabilmente abbastanza casuale. Inoltre il numero di città diverse è maggiore di 5 per cui non mi è chiaro cosa tu volessi davvero ottenere.
Il metodo corretto sarebbe stato scrivere qualcosa come
Il metodo corretto sarebbe stato scrivere qualcosa come
// napoli: 0, milano: 1, roma: 2, londra: 3, bergamo: 4, parigi: 5, firenze: 6 addEdge(graph, 0, 1); // napoli, milano addEdge(graph, 1, 2); // milano, roma addEdge(graph, 0, 3); // napoli, londra addEdge(graph, 4, 5); // bergamo, parigi addEdge(graph, 6, 2); // firenze, roma addEdge(graph, 1, 2); // milano, roma -- di nuovo?