Notes
Notes - notes.io |
#include <stdlib.h>
#include <limits.h>
#define MAX_VERTICES 100
typedef struct {
int num_vertices;
int matrix[MAX_VERTICES][MAX_VERTICES];
} Graph;
typedef struct {
int weight;
int parent;
} Vertex;
int find_min_vertex(Graph* graph, Vertex* vertices) {
int min_vertex = -1;
int min_weight = INT_MAX;
for (int i = 0; i < graph->num_vertices; i++) {
if (vertices[i].weight < min_weight && vertices[i].parent == -1) {
min_vertex = i;
min_weight = vertices[i].weight;
}
}
return min_vertex;
}
void print_matrix(Graph* graph) {
printf("Adjacency Matrix:n");
for (int i = 0; i < graph->num_vertices; i++) {
for (int j = 0; j < graph->num_vertices; j++) {
printf("%d ", graph->matrix[i][j]);
}
printf("n");
}
}
void prim(Graph* graph) {
Vertex vertices[MAX_VERTICES];
for (int i = 0; i < graph->num_vertices; i++) {
vertices[i].weight = INT_MAX;
vertices[i].parent = -1;
}
vertices[0].weight = 0;
for (int i = 0; i < graph->num_vertices; i++) {
int current_vertex = find_min_vertex(graph, vertices);
vertices[current_vertex].parent = current_vertex;
for (int j = 0; j < graph->num_vertices; j++) {
if (graph->matrix[current_vertex][j] > 0 && vertices[j].parent == -1 && graph->matrix[current_vertex][j] < vertices[j].weight) {
vertices[j].weight = graph->matrix[current_vertex][j];
vertices[j].parent = current_vertex;
}
}
}
printf("Minimum Spanning Tree:n");
int total_weight = 0;
for (int i = 1; i < graph->num_vertices; i++) {
printf("%d - %dt%dn", vertices[i].parent, i, vertices[i].weight);
total_weight += vertices[i].weight;
}
printf("Total Weight: %dn", total_weight);
}
int main() {
Graph graph;
graph.num_vertices = 0;
// Prompt the user to enter the number of vertices
printf("Enter the number of vertices: ");
scanf("%d", &graph.num_vertices);
// Prompt the user to enter the adjacency matrix
printf("Enter the adjacency matrix:n");
for (int i = 0; i < graph.num_vertices; i++) {
for (int j = 0; j < graph.num_vertices; j++) {
scanf("%d", &graph.matrix[i][j]);
}
}
// Print the adjacency matrix
print_matrix(&graph);
// Run Prim's algorithm and print the minimum spanning tree
prim(&graph);
return 0;
}
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team