added more grah algos & Makefile, fixed som return non-void func errors

This commit is contained in:
ttuanho 2020-01-17 20:10:31 +11:00
parent 807abcd0e8
commit fd52c9f2c6
13 changed files with 686 additions and 1 deletions

View File

@ -0,0 +1,102 @@
// Graph ADT
// Adjacency Matrix Representation
#include "Graph.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct GraphRep {
int **edges; // adjacency matrix
int nV; // #vertices
int nE; // #edges
} GraphRep;
Graph newGraph(int V) {
assert(V >= 0);
int i;
Graph g = malloc(sizeof(GraphRep));
assert(g != NULL);
g->nV = V;
g->nE = 0;
// allocate memory for each row
g->edges = malloc(V * sizeof(int *));
assert(g->edges != NULL);
// allocate memory for each column and initialise with 0
for (i = 0; i < V; i++) {
g->edges[i] = calloc(V, sizeof(int));
assert(g->edges[i] != NULL);
}
return g;
}
// check if vertex is valid in a graph
bool validV(Graph g, Vertex v) {
return (g != NULL && v >= 0 && v < g->nV);
}
void insertEdge(Graph g, Edge e) {
assert(g != NULL && validV(g,e.v) && validV(g,e.w));
if (!g->edges[e.v][e.w]) { // edge e not in graph
g->edges[e.v][e.w] = 1;
g->edges[e.w][e.v] = 1;
g->nE++;
}
}
void removeEdge(Graph g, Edge e) {
assert(g != NULL && validV(g,e.v) && validV(g,e.w));
if (g->edges[e.v][e.w]) { // edge e in graph
g->edges[e.v][e.w] = 0;
g->edges[e.w][e.v] = 0;
g->nE--;
}
}
bool adjacent(Graph g, Vertex v, Vertex w) {
assert(g != NULL && validV(g,v) && validV(g,w));
return (g->edges[v][w] != 0);
}
void showGraph(Graph g) {
assert(g != NULL);
int i, j;
printf("Number of vertices: %d\n", g->nV);
printf("Number of edges: %d\n", g->nE);
for (i = 0; i < g->nV; i++)
for (j = i+1; j < g->nV; j++)
if (g->edges[i][j])
printf("Edge %d - %d\n", i, j);
}
void freeGraph(Graph g) {
assert(g != NULL);
int i;
for (i = 0; i < g->nV; i++)
free(g->edges[i]);
free(g->edges);
free(g);
}
// By
// .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------.
// | .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. |
// | | _________ | || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____ | || | ____ | |
// | | | _ _ | | || ||_ _||_ _|| || | / \ | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | |
// | | |_/ | | \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | | |__| | | || | / .--. \ | |
// | | | | | || | | ' ' | | || | / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | |
// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_ | | | | _| | | |_ | || | \ `--' / | |
// | | |_____| | || | `.__.' | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.' | |
// | | | || | | || | | || | | | | | | || | | |
// | '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' |
// '----------------' '----------------' '----------------' '----------------' '----------------' '----------------'
// Email : z5261243@unsw.edu.au
// hhoanhtuann@gmail.com

View File

@ -0,0 +1,37 @@
// Graph ADT interface ... COMP2521
#include <stdbool.h>
typedef struct GraphRep *Graph;
// vertices are ints
typedef int Vertex;
// edges are pairs of vertices (end-points)
typedef struct Edge {
Vertex v;
Vertex w;
} Edge;
Graph newGraph(int);
void insertEdge(Graph, Edge);
void removeEdge(Graph, Edge);
bool adjacent(Graph, Vertex, Vertex);
void showGraph(Graph);
void freeGraph(Graph);
// By
// .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------.
// | .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. |
// | | _________ | || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____ | || | ____ | |
// | | | _ _ | | || ||_ _||_ _|| || | / \ | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | |
// | | |_/ | | \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | | |__| | | || | / .--. \ | |
// | | | | | || | | ' ' | | || | / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | |
// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_ | | | | _| | | |_ | || | \ `--' / | |
// | | |_____| | || | `.__.' | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.' | |
// | | | || | | || | | || | | | | | | || | | |
// | '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' |
// '----------------' '----------------' '----------------' '----------------' '----------------' '----------------'
// Email : z5261243@unsw.edu.au
// hhoanhtuann@gmail.com

View File

@ -0,0 +1,56 @@
CC=gcc
CFLAGS=-Wall -Werror -std=c99
all: BFS Bellman-Ford DFS Dijkstra Floyd-Warshall bfsQueue dfsRecursive euler hamiltonian strongly_connected_components topologicalSort transitiveClosure
BFS: BFS.c
$(CC) -o BFS BFS.c
Bellman-Ford: Bellman-Ford.c
$(CC) -o Bellman-Ford Bellman-Ford.c
DFS: DFS.c
$(CC) -o DFS DFS.c
Dijkstra: Dijkstra.c
$(CC) -o Dijkstra Dijkstra.c
Floyd-Warshall: Floyd-Warshall.c
$(CC) -o Floyd-Warshall Floyd-Warshall.c
Graph.o: Graph.c Graph.h
$(CC) $(CFLAGS) -c Graph.c
bfsQueue: Graph.o queue.o bfsQueue.o
$(CC) Graph.o queue.o bfsQueue.o -o bfsQueue
bfsQueue.o: bfsQueue.c
$(CC) $(CFLAGS) -c bfsQueue.c
dfsRecursive: Graph.o queue.o dfsRecursive.o
$(CC) Graph.o queue.o dfsRecursive.o -o dfsRecursive
dfsRecursive.o: dfsRecursive.c
$(CC) -c dfsRecursive.c
euler: Graph.o euler.o
$(CC) Graph.o euler.o -o euler
euler.o: euler.c
$(CC) $(CFLAGS) -c euler.c
hamiltonian: Graph.o hamiltonian.o
$(CC) Graph.o hamiltonian.o -o hamiltonian
hamiltonian.o: hamiltonian.c
$(CC) $(CFLAGS) -c hamiltonian.c
queue.o: queue.c queue.h
$(CC) $(CFLAGS) -c queue.c
strongly_connected_components: strongly_connected_components.c
$(CC) -o strongly_connected_components strongly_connected_components.c
topologicalSort: topologicalSort.c
$(CC) -o topologicalSort topologicalSort.c
transitiveClosure: transitiveClosure.c
$(CC) -o transitiveClosure transitiveClosure.c
# By
# .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------.
# | .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. |
# | | _________ | || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____ | || | ____ | |
# | | | _ _ | | || ||_ _||_ _|| || | / \ | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | |
# | | |_/ | | \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | | |__| | | || | / .--. \ | |
# | | | | | || | | ' ' | | || | / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | |
# | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_ | | | | _| | | |_ | || | \ `--' / | |
# | | |_____| | || | `.__.' | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.' | |
# | | | || | | || | | || | | | | | | || | | |
# | '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' |
# '----------------' '----------------' '----------------' '----------------' '----------------' '----------------'
# Email : z5261243@unsw.edu.au
# hhoanhtuann@gmail.com

View File

@ -0,0 +1,81 @@
#include <stdio.h>
#include <stdbool.h>
#include "queue.h"
#include "Graph.h"
#define MAX_NODES 1000
int visited[MAX_NODES]; // array to store visiting order
// indexed by vertex 0..nV-1
bool findPathBFS(Graph g, int nV, Vertex src, Vertex dest) {
Vertex v;
for (v = 0; v < nV; v++)
visited[v] = -1;
visited[src] = src;
queue Q = newQueue();
QueueEnqueue(Q, src);
while (!QueueIsEmpty(Q)) {
v = QueueDequeue(Q);
Vertex w;
for (w = 0; w < nV; w++)
if (adjacent(g, v, w) && visited[w] == -1) {
visited[w] = v;
if (w == dest)
return true;
else
QueueEnqueue(Q, w);
}
}
return false;
}
int main(void) {
int V = 10;
Graph g = newGraph(V);
Edge e;
e.v = 0; e.w = 1; insertEdge(g, e);
e.v = 0; e.w = 2; insertEdge(g, e);
e.v = 0; e.w = 5; insertEdge(g, e);
e.v = 1; e.w = 5; insertEdge(g, e);
e.v = 2; e.w = 3; insertEdge(g, e);
e.v = 3; e.w = 4; insertEdge(g, e);
e.v = 3; e.w = 5; insertEdge(g, e);
e.v = 3; e.w = 8; insertEdge(g, e);
e.v = 4; e.w = 5; insertEdge(g, e);
e.v = 4; e.w = 7; insertEdge(g, e);
e.v = 4; e.w = 8; insertEdge(g, e);
e.v = 5; e.w = 6; insertEdge(g, e);
e.v = 7; e.w = 8; insertEdge(g, e);
e.v = 7; e.w = 9; insertEdge(g, e);
e.v = 8; e.w = 9; insertEdge(g, e);
int src = 0, dest = 6;
if (findPathBFS(g, V, src, dest)) {
Vertex v = dest;
while (v != src) {
printf("%d - ", v);
v = visited[v];
}
printf("%d\n", src);
}
return 0;
}
// By
// .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------.
// | .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. |
// | | _________ | || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____ | || | ____ | |
// | | | _ _ | | || ||_ _||_ _|| || | / \ | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | |
// | | |_/ | | \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | | |__| | | || | / .--. \ | |
// | | | | | || | | ' ' | | || | / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | |
// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_ | | | | _| | | |_ | || | \ `--' / | |
// | | |_____| | || | `.__.' | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.' | |
// | | | || | | || | | || | | | | | | || | | |
// | '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' |
// '----------------' '----------------' '----------------' '----------------' '----------------' '----------------'
// Email : z5261243@unsw.edu.au
// hhoanhtuann@gmail.com

View File

@ -0,0 +1,74 @@
#include <stdio.h>
#include <stdbool.h>
#include "Graph.h"
#define MAX_NODES 1000
int visited[MAX_NODES]; // array to store visiting order
// indexed by vertex 0..nV-1
bool dfsPathCheck(Graph g, int nV, Vertex v, Vertex dest) {
Vertex w;
for (w = 0; w < nV; w++)
if (adjacent(g, v, w) && visited[w] == -1) {
visited[w] = v;
if (w == dest)
return true;
else if (dfsPathCheck(g, nV, w, dest))
return true;
}
return false;
}
bool findPathDFS(Graph g, int nV, Vertex src, Vertex dest) {
Vertex v;
for (v = 0; v < nV; v++)
visited[v] = -1;
visited[src] = src;
return dfsPathCheck(g, nV, src, dest);
}
int main(void) {
int V = 6;
Graph g = newGraph(V);
Edge e;
e.v = 0; e.w = 1; insertEdge(g, e);
e.v = 0; e.w = 4; insertEdge(g, e);
e.v = 0; e.w = 5; insertEdge(g, e);
e.v = 5; e.w = 4; insertEdge(g, e);
e.v = 4; e.w = 2; insertEdge(g, e);
e.v = 4; e.w = 3; insertEdge(g, e);
e.v = 5; e.w = 3; insertEdge(g, e);
e.v = 1; e.w = 2; insertEdge(g, e);
e.v = 3; e.w = 2; insertEdge(g, e);
int src = 0, dest = 5;
if (findPathDFS(g, V, src, dest)) {
Vertex v = dest;
while (v != src) {
printf("%d - ", v);
v = visited[v];
}
printf("%d\n", src);
}
return 0;
}
// By
// .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------.
// | .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. |
// | | _________ | || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____ | || | ____ | |
// | | | _ _ | | || ||_ _||_ _|| || | / \ | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | |
// | | |_/ | | \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | | |__| | | || | / .--. \ | |
// | | | | | || | | ' ' | | || | / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | |
// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_ | | | | _| | | |_ | || | \ `--' / | |
// | | |_____| | || | `.__.' | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.' | |
// | | | || | | || | | || | | | | | | || | | |
// | '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' |
// '----------------' '----------------' '----------------' '----------------' '----------------' '----------------'
// Email : z5261243@unsw.edu.au
// hhoanhtuann@gmail.com

View File

@ -0,0 +1,82 @@
#include <stdio.h>
#include <stdbool.h>
#include "Graph.h"
// Return the number of vertices that v is
// connected to
int degree(Graph g, int nV, Vertex v) {
int deg = 0;
Vertex w;
for (w = 0; w < nV; w++)
if (adjacent(g, v, w))
deg++;
return deg;
}
// If start from vertex v, decide if the
// graph has euler path
bool hasEulerPath(Graph g, int nV, Vertex v, Vertex w) {
if (v != w) {
if (degree(g, nV, v) % 2 == 0 || degree(g, nV, w) % 2 == 0)
return false;
} else if (degree(g, nV, v) % 2 != 0) {
return false;
}
Vertex x;
for (x = 0; x < nV; x++)
if (x != v && x != w && degree(g, nV, x) % 2 != 0)
return false;
return true;
}
int main(void) {
Edge e;
int n;
printf("Enter the number of vertices: ");
scanf("%d", &n);
Graph g = newGraph(n);
Vertex src, dest;
printf("Enter source node: ");
scanf("%d", &src);
printf("Enter destination node: ");
scanf("%d", &dest);
printf("Enter an edge (from): ");
while (scanf("%d", &e.v) == 1) {
printf("Enter an edge (to): ");
scanf("%d", &e.w);
insertEdge(g, e);
printf("Enter an edge (from): ");
}
printf("Finished.\n");
printf("The graph has ");
if (hasEulerPath(g, n, src, dest))
printf("an");
else
printf("no");
printf(" Euler path from %d to %d.\n", src, dest);
freeGraph(g);
return 0;
}
// By
// .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------.
// | .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. |
// | | _________ | || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____ | || | ____ | |
// | | | _ _ | | || ||_ _||_ _|| || | / \ | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | |
// | | |_/ | | \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | | |__| | | || | / .--. \ | |
// | | | | | || | | ' ' | | || | / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | |
// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_ | | | | _| | | |_ | || | \ `--' / | |
// | | |_____| | || | `.__.' | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.' | |
// | | | || | | || | | || | | | | | | || | | |
// | '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' |
// '----------------' '----------------' '----------------' '----------------' '----------------' '----------------'
// Email : z5261243@unsw.edu.au
// hhoanhtuann@gmail.com

View File

@ -0,0 +1,87 @@
#include <stdio.h>
#include <stdbool.h>
#include "Graph.h"
#define MAX_NODES 1000
bool visited[MAX_NODES];
bool hamiltonR(Graph g, int nV, Vertex v, Vertex dest, int d) {
// v = current vertex considered
// dest = destination vertex
// d = distance "remaining" until path found
Vertex w;
if (v == dest) {
return (d == 0);
} else {
visited[v] = true;
for (w = 0; w < nV; w++) {
if (adjacent(g, v, w) && !visited[w]) {
if (hamiltonR(g, nV, w, dest, d-1)) {
return true;
}
}
}
}
visited[v] = false;
return false;
}
bool hasHamiltonianPath(Graph g, int nV, Vertex src, Vertex dest) {
Vertex v;
for (v = 0; v < nV; v++)
visited[v] = false;
return hamiltonR(g, nV, src, dest, nV-1);
}
int main(void) {
Edge e;
int n;
printf("Enter the number of vertices: ");
scanf("%d", &n);
Graph g = newGraph(n);
Vertex src, dest;
printf("Enter source node: ");
scanf("%d", &src);
printf("Enter destination node: ");
scanf("%d", &dest);
printf("Enter an edge (from): ");
while (scanf("%d", &e.v) == 1) {
printf("Enter an edge (to): ");
scanf("%d", &e.w);
insertEdge(g, e);
printf("Enter an edge (from): ");
}
printf("Finished.\n");
printf("The graph has ");
if (hasHamiltonianPath(g, n, src, dest))
printf("a");
else
printf("no");
printf(" Hamiltonian path from %d to %d.\n", src, dest);
freeGraph(g);
return 0;
}
// By
// .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------.
// | .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. |
// | | _________ | || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____ | || | ____ | |
// | | | _ _ | | || ||_ _||_ _|| || | / \ | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | |
// | | |_/ | | \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | | |__| | | || | / .--. \ | |
// | | | | | || | | ' ' | | || | / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | |
// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_ | | | | _| | | |_ | || | \ `--' / | |
// | | |_____| | || | `.__.' | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.' | |
// | | | || | | || | | || | | | | | | || | | |
// | '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' |
// '----------------' '----------------' '----------------' '----------------' '----------------' '----------------'
// Email : z5261243@unsw.edu.au
// hhoanhtuann@gmail.com

View File

@ -27,7 +27,7 @@ struct Graph
// Creates a graph with V vertices and E edges
struct Graph* createGraph(int V, int E)
{
struct Graph* graph = new Graph;
struct Graph* graph = new Graph();
graph->V = V;
graph->E = E;

View File

@ -0,0 +1,88 @@
// Queue ADT implementation ... COMP2521
#include <stdlib.h>
#include <assert.h>
#include "queue.h"
typedef struct node {
int data;
struct node *next;
} NodeT;
typedef struct QueueRep {
int length;
NodeT *head;
NodeT *tail;
} QueueRep;
// set up empty queue
queue newQueue() {
queue Q = malloc(sizeof(QueueRep));
Q->length = 0;
Q->head = NULL;
Q->tail = NULL;
return Q;
}
// remove unwanted queue
void dropQueue(queue Q) {
NodeT *curr = Q->head;
while (curr != NULL) {
NodeT *temp = curr->next;
free(curr);
curr = temp;
}
free(Q);
}
// check whether queue is empty
int QueueIsEmpty(queue Q) {
return (Q->length == 0);
}
// insert an int at end of queue
void QueueEnqueue(queue Q, int v) {
NodeT *new = malloc(sizeof(NodeT));
assert(new != NULL);
new->data = v;
new->next = NULL;
if (Q->tail != NULL) {
Q->tail->next = new;
Q->tail = new;
} else {
Q->head = new;
Q->tail = new;
}
Q->length++;
}
// remove int from front of queue
int QueueDequeue(queue Q) {
assert(Q->length > 0);
NodeT *p = Q->head;
Q->head = Q->head->next;
if (Q->head == NULL) {
Q->tail = NULL;
}
Q->length--;
int d = p->data;
free(p);
return d;
}
// By
// .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------.
// | .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. |
// | | _________ | || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____ | || | ____ | |
// | | | _ _ | | || ||_ _||_ _|| || | / \ | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | |
// | | |_/ | | \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | | |__| | | || | / .--. \ | |
// | | | | | || | | ' ' | | || | / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | |
// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_ | | | | _| | | |_ | || | \ `--' / | |
// | | |_____| | || | `.__.' | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.' | |
// | | | || | | || | | || | | | | | | || | | |
// | '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' |
// '----------------' '----------------' '----------------' '----------------' '----------------' '----------------'
// Email : z5261243@unsw.edu.au
// hhoanhtuann@gmail.com

View File

@ -0,0 +1,26 @@
// Queue ADT header file ... COMP2521
typedef struct QueueRep *queue;
queue newQueue(); // set up empty queue
void dropQueue(queue); // remove unwanted queue
int QueueIsEmpty(queue); // check whether queue is empty
void QueueEnqueue(queue, int); // insert an int at end of queue
int QueueDequeue(queue); // remove int from front of queue
// By
// .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------.
// | .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. |
// | | _________ | || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____ | || | ____ | |
// | | | _ _ | | || ||_ _||_ _|| || | / \ | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | |
// | | |_/ | | \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | | |__| | | || | / .--. \ | |
// | | | | | || | | ' ' | | || | / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | |
// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_ | | | | _| | | |_ | || | \ `--' / | |
// | | |_____| | || | `.__.' | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.' | |
// | | | || | | || | | || | | | | | | || | | |
// | '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' |
// '----------------' '----------------' '----------------' '----------------' '----------------' '----------------'
// Email : z5261243@unsw.edu.au
// hhoanhtuann@gmail.com

View File

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 40//Assume 40 nodes at max in graph
#define INT_MIN 0
//A vertex of the graph
struct node
{
@ -195,6 +196,7 @@ struct Stack* createStack()
{
struct Stack* stack=malloc(sizeof(struct Stack));
stack->top=-1;
return stack;
}
//Pushes element into stack
void push(struct Stack* stack,int element)

View File

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 40//Assume 40 nodes at max in graph
#define INT_MIN 0
//A vertex of the graph
struct node
{
@ -147,6 +148,7 @@ struct Stack* createStack()
{
struct Stack* stack=malloc(sizeof(struct Stack));
stack->top=-1;
return stack;
}
//Pushes element into stack
void push(struct Stack* stack,int element)

View File

@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdbool.h>
#define NODES 4
int digraph[NODES][NODES]={ {0,1,1,1}, {1,0,1,0}, {0,1,0,0}, {0,0,0,0} };
int tc[NODES][NODES];
void warshall() {
int i, s, t;
for (s = 0; s < NODES; s++)
for (t = 0; t < NODES; t++)
tc[s][t] = digraph[s][t];
for (i = 0; i < NODES; i++)
for (s = 0; s < NODES; s++)
for (t = 0; t < NODES; t++)
if (tc[s][i] && tc[i][t])
tc[s][t] = 1;
}
int main(void) {
warshall();
int i, j;
for (i = 0; i < NODES; i++) {
for (j = 0; j < NODES; j++) {
printf("%d ", tc[i][j]);
}
putchar('\n');
}
return 0;
}
// By
// .----------------. .----------------. .----------------. .-----------------. .----------------. .----------------.
// | .--------------. || .--------------. || .--------------. || .--------------. | | .--------------. || .--------------. |
// | | _________ | || | _____ _____ | || | __ | || | ____ _____ | | | | ____ ____ | || | ____ | |
// | | | _ _ | | || ||_ _||_ _|| || | / \ | || ||_ \|_ _| | | | | |_ || _| | || | .' `. | |
// | | |_/ | | \_| | || | | | | | | || | / /\ \ | || | | \ | | | | | | | |__| | | || | / .--. \ | |
// | | | | | || | | ' ' | | || | / ____ \ | || | | |\ \| | | | | | | __ | | || | | | | | | |
// | | _| |_ | || | \ `--' / | || | _/ / \ \_ | || | _| |_\ |_ | | | | _| | | |_ | || | \ `--' / | |
// | | |_____| | || | `.__.' | || ||____| |____|| || ||_____|\____| | | | | |____||____| | || | `.____.' | |
// | | | || | | || | | || | | | | | | || | | |
// | '--------------' || '--------------' || '--------------' || '--------------' | | '--------------' || '--------------' |
// '----------------' '----------------' '----------------' '----------------' '----------------' '----------------'
// Email : z5261243@unsw.edu.au
// hhoanhtuann@gmail.com