We present the Edge Weighted Graph ADT here:
/**
* Weighted edge data type.
*/
#include <stdlib.h>
#include <float.h>
#include <limits.h>
#include <stdio.h>
#ifndef WEIGHTED_EDGE
#define WEIGHTED_EDGE
typedef struct {
int v, w;
double weight;
}*WeightedEdge;
WeightedEdge WeightedEdgeCreate(int v, int w, double weight) {
WeightedEdge we = (WeightedEdge)malloc(sizeof(*we));
we->v = v;
we->w = w;
we->weight = weight;
return we;
}
double WeightedEdgeWeight(WeightedEdge we) { return we != NULL ? we->weight : DBL_MIN; }
int WeightedEdgeEither(WeightedEdge we) { return we != NULL ? we->v : INT_MIN; }
int WeightedEdgeOther(WeightedEdge we, int v) {
if (we->v == v) {
return we->w;
} else if (we->w == v) {
return we->v;
} else {
return INT_MIN;
}
}
int WeightedEdgeAscendingCompare(WeightedEdge we1, WeightedEdge we2) {
double c = we1->weight - we2->weight;
if (c > .0f) {
return 1;
} else if (c < .0f) {
return -1;
} else {
return 0;
}
}
int WeightedEdgeDescendingCompare(WeightedEdge we1, WeightedEdge we2) { return WeightedEdgeAscendingCompare(we2, we1); }
void WeightedEdgePrint(WeightedEdge we) { if (we != NULL) { printf("%d-%d, %.2f", we->v, we->w, we->weight); } }
#endif
/**
* Edge weighted graph data type.
* The EdgeWeightedGraphCreateFromInput function takes input of the form:
* 8
* 16
* 4 5 0.35
* 4 7 0.37
* 5 7 0.28
* 0 7 0.16
* 1 5 0.32
* 0 4 0.38
* 2 3 0.17
* 1 7 0.19
* 0 2 0.26
* 1 2 0.36
* 1 3 0.29
* 2 7 0.34
* 6 2 0.40
* 3 6 0.52
* 6 0 0.58
* 6 4 0.93
*/
#include "weighted_edge.c"
#include "linked_list_bag.c"
#ifndef EDGE_WEIGHTED_GRAPH
#define EDGE_WEIGHTED_GRAPH
typedef struct {
int e;
int v;
LinkedListBag *adj;
}*EdgeWeightedGraph;
void EdgeWeightedGraphRelease(EdgeWeightedGraph ewg) {
if (ewg == NULL) return;
for (int v = 0; v < ewg->v; v++) { LinkedListBagRelease(ewg->adj[v]); }
free(ewg->adj);
free(ewg);
}
void EdgeWeightedGraphAddEdge(EdgeWeightedGraph ewg, WeightedEdge we) {
int v = WeightedEdgeEither(we), w = WeightedEdgeOther(we, v);
LinkedListBagAdd(ewg->adj[v], we);
LinkedListBagAdd(ewg->adj[w], we);
ewg->e++;
}
EdgeWeightedGraph EdgeWeightedGraphCreate(int v) {
EdgeWeightedGraph ewg = (EdgeWeightedGraph)malloc(sizeof(*ewg));
ewg->v = v;
ewg->e = 0;
ewg->adj = (LinkedListBag*)malloc(v * sizeof(LinkedListBag));
for (int i = 0; i < v; i++) { *(ewg->adj + i) = LinkedListBagCreate(); }
return ewg;
}
EdgeWeightedGraph EdgeWeightedGraphCreateFromInput() {
int v, e;
scanf("%i%i", &v, &e);
EdgeWeightedGraph ewg = EdgeWeightedGraphCreate(v);
for (int i = 0; i < e; i++) {
int v, w;
double weight;
scanf("%i%i%lf", &v, &w, &weight);
WeightedEdge we = WeightedEdgeCreate(v, w, weight);
EdgeWeightedGraphAddEdge(ewg, we);
}
return ewg;
}
int EdgeWeightedGraphNumberOfEdges(EdgeWeightedGraph ewg) { return ewg != NULL ? ewg->e : INT_MIN; }
int EdgeWeightedGraphNumberOfVertices(EdgeWeightedGraph ewg) { return ewg != NULL ? ewg->v : INT_MIN; }
WeightedEdge* EdgeWeightedGraphAdj(EdgeWeightedGraph ewg, int v, int *n) { return (WeightedEdge*)LinkedListBagItems(ewg->adj[v], n); }
WeightedEdge* EdgeWeightedGraphEdges(EdgeWeightedGraph ewg) {
if (ewg == NULL) return NULL;
LinkedListBag edges = LinkedListBagCreate();
for (int v = 0; v < ewg->v; v++) {
int n = 0;
WeightedEdge *adj = EdgeWeightedGraphAdj(ewg, v, &n);
for (int i = 0; i < n; i++) { if (WeightedEdgeOther(adj[i], v) > v) { LinkedListBagAdd(edges, adj[i]); } }
}
WeightedEdge *ret = LinkedListBagItems(edges, NULL);
LinkedListBagRelease(edges);
return ret;
}
void EdgeWeightedGraphPrint(EdgeWeightedGraph ewg) {
if (ewg == NULL) return;
for (int v = 0; v < ewg->v; v++) {
printf("%i: ", v);
LinkedListBagPrint(ewg->adj[v], (void(*)(void*))WeightedEdgePrint);
printf("\n");
}
}
#endif
网友评论