Add graph references

This commit is contained in:
Abdelrahman Said
2026-06-28 13:49:01 +01:00
parent 0a9807e448
commit a11edf0c53
2578 changed files with 868045 additions and 0 deletions
+152
View File
@@ -0,0 +1,152 @@
// vim:fileencoding=utf-8:foldmethod=marker
/* graph.c
A generic adjacency list graph data type.
*/
/* Copyright 2003-2020 by Steven S. Skiena; all rights reserved.
Permission is granted for use in non-commerical applications
provided this copyright notice remains intact and unchanged.
These programs appear in my books:
"The Algorithm Design Manual" by Steven Skiena, second edition, Springer,
London 2008. See out website www.algorist.com for additional information
or https://www.amazon.com/exec/obidos/ASIN/1848000693/thealgorith01-20
"Programming Challenges: The Programming Contest Training Manual"
by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
See our website www.programming-challenges.com for additional information,
or https://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
*/
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
#include "graph.h"
/* [[[ init_graph_c */
void initialize_graph(graph *g, bool directed) {
int i; /* counter */
g->nvertices = 0;
g->nedges = 0;
g->directed = directed;
for (i = 1; i <= MAXV; i++) {
g->degree[i] = 0;
}
for (i = 1; i <= MAXV; i++) {
g->edges[i] = NULL;
}
}
/* ]]] */
/* [[[ insert_edge_cut */
void insert_edge(graph *g, int x, int y, bool directed) {
edgenode *p; /* temporary pointer */
p = malloc(sizeof(edgenode)); /* allocate edgenode storage */
p->weight = 0;
p->y = y;
p->next = g->edges[x];
g->edges[x] = p; /* insert at head of list */
g->degree[x]++;
if (!directed) {
insert_edge(g, y, x, true);
} else {
g->nedges++;
}
}
/* ]]] */
/* [[[ read_graph_cut */
void read_graph(graph *g, bool directed) {
int i; /* counter */
int m; /* number of edges */
int x, y; /* vertices in edge (x,y) */
initialize_graph(g, directed);
scanf("%d %d", &(g->nvertices), &m);
for (i = 1; i <= m; i++) {
scanf("%d %d", &x, &y);
insert_edge(g, x, y, directed);
}
}
/* ]]] */
void delete_edge(graph *g, int x, int y, bool directed) {
edgenode *p, *p_back; /* temporary pointers */
p = g->edges[x];
p_back = NULL;
while (p != NULL) {
if (p->y == y) {
g->degree[x]--;
if (p_back != NULL) {
p_back->next = p->next;
} else {
g->edges[x] = p->next;
}
free(p);
if (!directed) {
delete_edge(g, y, x, true);
} else {
g->nedges--;
}
return;
} else {
p = p->next;
}
}
printf("Warning: deletion(%d,%d) not found in g.\n",x,y);
}
/* [[[ print_graph_cut */
void print_graph(graph *g) {
int i; /* counter */
edgenode *p; /* temporary pointer */
for (i = 1; i <= g->nvertices; i++) {
printf("%d: ", i);
p = g->edges[i];
while (p != NULL) {
printf(" %d", p->y);
p = p->next;
}
printf("\n");
}
}
/* ]]] */
/* [[[ graph_transpose_cut */
graph *transpose(graph *g) {
graph *gt; /* transpose of graph g */
int x; /* counter */
edgenode *p; /* temporary pointer */
gt = (graph *) malloc(sizeof(graph));
initialize_graph(gt, true); /* initialize directed graph */
gt->nvertices = g->nvertices;
for (x = 1; x <= g->nvertices; x++) {
p = g->edges[x];
while (p != NULL) {
insert_edge(gt, p->y, x, true);
p = p->next;
}
}
return(gt);
}
/* ]]] */
+71
View File
@@ -0,0 +1,71 @@
// vim:fileencoding=utf-8:foldmethod=marker
#ifndef GRAPH_H
#define GRAPH_H
/* graph.h
Header file for pointer-based graph data type
*/
/* Copyright 2003-2020 by Steven S. Skiena; all rights reserved.
Permission is granted for use in non-commerical applications
provided this copyright notice remains intact and unchanged.
These programs appear in my books:
"The Algorithm Design Manual" by Steven Skiena, second edition, Springer,
London 2008. See out website www.algorist.com for additional information
or https://www.amazon.com/exec/obidos/ASIN/1848000693/thealgorith01-20
"Programming Challenges: The Programming Contest Training Manual"
by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
See our website www.programming-challenges.com for additional information,
or https://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
*/
#include <stdbool.h>
/* #define _NULL 0 null pointer */
/* DFS edge types */
#define TREE 0 /* tree edge */
#define BACK 1 /* back edge */
#define CROSS 2 /* cross edge */
#define FORWARD 3 /* forward edge */
/* [[[ graph_struct_cut */
/* [[[ maxv_cut */
#define MAXV 100 /* maximum number of vertices */
/* ]]] */
/* [[[ edge_struct_only_cut */
typedef struct edgenode {
int y; /* adjacency info */
int weight; /* edge weight, if any */
struct edgenode *next; /* next edge in list */
} edgenode;
/* ]]] */
/* [[[ graph_struct_only_cut */
typedef struct {
edgenode *edges[MAXV+1]; /* adjacency info */
int degree[MAXV+1]; /* outdegree of each vertex */
int nvertices; /* number of vertices in the graph */
int nedges; /* number of edges in the graph */
int directed; /* is the graph directed? */
} graph;
/* ]]] */
/* ]]] */
void process_vertex_early(int v);
void process_vertex_late(int v);
void process_edge(int x, int y);
void initialize_graph(graph *g, bool directed);
void read_graph(graph *g, bool directed);
void print_graph(graph *g);
graph *transpose(graph *g);
#endif // !GRAPH_H
+29
View File
@@ -0,0 +1,29 @@
// vim:fileencoding=utf-8:foldmethod=marker
#ifndef ITEM_H
#define ITEM_H
/* item.h
Header file for linked implementation
*/
/* Copyright 2003-2020 by Steven S. Skiena; all rights reserved.
Permission is granted for use in non-commerical applications
provided this copyright notice remains intact and unchanged.
These programs appear in my books:
"The Algorithm Design Manual" by Steven Skiena, second edition, Springer,
London 2008. See out website www.algorist.com for additional information
or https://www.amazon.com/exec/obidos/ASIN/1848000693/thealgorith01-20
"Programming Challenges: The Programming Contest Training Manual"
by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
See our website www.programming-challenges.com for additional information,
or https://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
*/
typedef int item_type;
#endif // !ITEM_H
+80
View File
@@ -0,0 +1,80 @@
// vim:fileencoding=utf-8:foldmethod=marker
/* queue.c
Implementation of a FIFO queue abstract data type.
*/
/* Copyright 2003-2020 by Steven S. Skiena; all rights reserved.
Permission is granted for use in non-commerical applications
provided this copyright notice remains intact and unchanged.
These programs appear in my books:
"The Algorithm Design Manual" by Steven Skiena, second edition, Springer,
London 2008. See out website www.algorist.com for additional information
or https://www.amazon.com/exec/obidos/ASIN/1848000693/thealgorith01-20
"Programming Challenges: The Programming Contest Training Manual"
by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
See our website www.programming-challenges.com for additional information,
or https://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
*/
#include <stdio.h>
#include <stdbool.h>
#include "queue.h"
void init_queue(queue *q) {
q->first = 0;
q->last = QUEUESIZE - 1;
q->count = 0;
}
void enqueue(queue *q, item_type x) {
if (q->count >= QUEUESIZE) {
printf("Warning: queue overflow enqueue x=%d\n", x);
} else {
q->last = (q->last + 1) % QUEUESIZE;
q->q[q->last] = x;
q->count = q->count + 1;
}
}
item_type dequeue(queue *q) {
item_type x;
if (q->count <= 0) printf("Warning: empty queue dequeue.\n");
x = q->q[q->first];
q->first = (q->first + 1) % QUEUESIZE;
q->count = q->count - 1;
return(x);
}
item_type headq(queue *q) {
return(q->q[q->first]);
}
int empty_queue(queue *q) {
if (q->count <= 0) {
return (true);
}
return (false);
}
void print_queue(queue *q) {
int i;
i = q->first;
while (i != q->last) {
printf("%d ", q->q[i]);
i = (i + 1) % QUEUESIZE;
}
printf("%2d ", q->q[i]);
printf("\n");
}
+45
View File
@@ -0,0 +1,45 @@
// vim:fileencoding=utf-8:foldmethod=marker
#ifndef QUEUE_H
#define QUEUE_H
/* queue.h
Header file for queue implementation
*/
/* Copyright 2003-2020 by Steven S. Skiena; all rights reserved.
Permission is granted for use in non-commerical applications
provided this copyright notice remains intact and unchanged.
These programs appear in my books:
"The Algorithm Design Manual" by Steven Skiena, second edition, Springer,
London 2008. See out website www.algorist.com for additional information
or https://www.amazon.com/exec/obidos/ASIN/1848000693/thealgorith01-20
"Programming Challenges: The Programming Contest Training Manual"
by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
See our website www.programming-challenges.com for additional information,
or https://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
*/
#include "item.h"
#define QUEUESIZE 1000
typedef struct {
item_type q[QUEUESIZE+1]; /* body of queue */
int first; /* position of first element */
int last; /* position of last element */
int count; /* number of queue elements */
} queue;
void init_queue(queue *q);
void enqueue(queue *q, item_type x);
item_type dequeue(queue *q);
item_type headq(queue *q);
int empty_queue(queue *q);
void print_queue(queue *q);
#endif // !QUEUE_H