Paste
Copy
Cut
Options

¡Tu solución está lista!

Nuestra ayuda de expertos desglosó tu problema en una solución confiable y fácil de entender.

Mira la respuesta
  • Pregunta: Crear un programa C donde muestre con una matriz de adyacencia grafos ponderados con arcos simulando lasestaciones de un metro y le pregunte al usuario a que estación quiere llegar y muestre al usuario en la consola la rutamás corta usando los arcos de los grafos, utiliza el algoritmo de dijkstra

    Crear un programa C donde muestre con una matriz de adyacencia grafos ponderados con arcos simulando las
    estaciones de un metro y le pregunte al usuario a que estación quiere llegar y muestre al usuario en la consola la ruta
    más corta usando los arcos de los grafos, utiliza el algoritmo de dijkstra
    student submitted image, transcription available
  • Chegg Logo
    Hay 3 pasos para resolver este problema.
    Solución
    Paso 1

    Aquí está el código en C que completa todos los requisitos indicados en la pregunta:

    #include <stdio.h> #include <limits.h> #define STATIONS 5 // Define the number of stations // Declare the adjacency matrix for the graph int graph[STATIONS][STATIONS]; // Function to create and initialize the graph void createGraph() { // Initialize all distances in the graph to INT_MAX (representing no connection) for (int i = 0; i < STATIONS; i++) for (int j = 0; j < STATIONS; j++) graph[i][j] = (i == j) ? 0 : INT_MAX; // Define weights (distances) for the edges here // Example: graph[0][1] = 10; means an edge between station 0 and 1 with weight 10 graph[0][1] = 10; graph[1][0] = 10; // Edge between station 0 and 1 graph[0][2] = 15; graph[2][0] = 15; // Edge between station 0 and 2 graph[0][3] = 20; graph[3][0] = 20; // Edge between station 0 and 3 graph[0][4] = 25; graph[4][0] = 25; // Edge between station 0 and 4 graph[1][2] = 30; graph[2][1] = 30; // Edge between station 1 and 2 graph[1][3] = 35; graph[3][1] = 35; // Edge between station 1 and 3 graph[1][4] = 40; graph[4][1] = 40; // Edge between station 1 and 4 graph[2][3] = 45; graph[3][2] = 45; // Edge between station 2 and 3 graph[2][4] = 50; graph[4][2] = 50; // Edge between station 2 and 4 graph[3][4] = 55; graph[4][3] = 55; // Edge between station 3 and 4 } // Function to find the vertex with minimum distance which is not yet included in shortest path tree int minDistance(int dist[], int visited[]) { int min = INT_MAX, min_index; for (int v = 0; v < STATIONS; v++) if (visited[v] == 0 && dist[v] <= min) min = dist[v], min_index = v; return min_index; } // Function to implement Dijkstra's algorithm void dijkstra(int src, int dist[], int prev[]) { int visited[STATIONS]; // Initialize all distances as INFINITE and visited[] as false for (int i = 0; i < STATIONS; i++) { dist[i] = INT_MAX; visited[i] = 0; prev[i] = -1; // Initializing previous array for path reconstruction } // Distance of source vertex from itself is always 0 dist[src] = 0; // Find shortest path for all vertices for (int count = 0; count < STATIONS - 1; count++) { // Pick the minimum distance vertex from the set of vertices not yet processed int u = minDistance(dist, visited); // Mark the picked vertex as processed visited[u] = 1; // Update dist value of the adjacent vertices of the picked vertex for (int v = 0; v < STATIONS; v++) if (!visited[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) { dist[v] = dist[u] + graph[u][v]; prev[v] = u; // Storing the predecessor of each vertex } } } // Function to print the constructed distance array void printPath(int prev[], int j) { if (prev[j] == -1) return; printPath(prev, prev[j]); printf("%d ", j); } // Main function int main() { int dist[STATIONS], prev[STATIONS]; createGraph(); // Create the graph with your metro stations // Debug: Print the graph printf("Graph:\n"); for (int i = 0; i < STATIONS; i++) { for (int j = 0; j < STATIONS; j++) { if (graph[i][j] == INT_MAX) printf("INF "); else printf("%d ", graph[i][j]); } printf("\n"); } // Set the source station (example: 0) int src = 0; dijkstra(src, dist, prev); // Run Dijkstra's algorithm // Ask user for the destination station printf("Enter destination station (0 to %d): ", STATIONS - 1); int destination; scanf("%d", &destination); // Display the shortest path from source to destination printf("Shortest path from station %d to station %d: %d ", src, destination, src); printPath(prev, destination); printf("\n"); return 0; }


    Explanation:

    Este programa en...

    Mira la respuesta completa
    answer image blur
    Paso 2
    Desbloquea
    Paso 3
    Desbloquea
    Respuesta
    Desbloquea