Files
  • main.c
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>

typedef struct UndirectedGraph UndirectedGraph;
struct UndirectedGraph {
	int vertexes;
	int ** array;
};

UndirectedGraph * make_graph(int vertexes) {
	UndirectedGraph * graph = (UndirectedGraph *) malloc(sizeof(UndirectedGraph *));

	graph->vertexes = vertexes;

	// making a 2D array
	graph->array = (int **) malloc(vertexes * sizeof(int *));

	int i, j;
	for (i = 0; i < vertexes; i++) {
		graph->array[i] = (int *) malloc(vertexes * sizeof(int));

		// initializing every element with 0
		for (j = 0; j < vertexes; j++) {
			graph->array[i][j] = 0;
		}
	}

	return graph;
}

void add_edge(UndirectedGraph * graph, int vertex1, int vertex2) {
	graph->array[vertex1][vertex2] = 1;
	graph->array[vertex2][vertex1] = 1;
}

void delete_edge(UndirectedGraph * graph, int vertex1, int vertex2) {
	graph->array[vertex1][vertex2] = 0;
	graph->array[vertex2][vertex1] = 0;
}

void print_array(UndirectedGraph * graph) {
	int i, j;
	for (i = 0; i < graph->vertexes; i++) {
		for (j = 0; j < graph->vertexes; j++) {
			printf("%d ", graph->array[i][j]);
		}
		printf("\n");
	}
}

int main() {

	/*
				0
			/		\
		1	---- 2
  */

	int number_of_vertices = 3;
	UndirectedGraph * graph = make_graph(number_of_vertices);
	add_edge(graph, 0, 1);
  add_edge(graph, 0, 2);
  add_edge(graph, 1, 2);
  print_array(graph);

  printf("deletion of edge from 0 to 1\n");
  delete_edge(graph, 0, 1);
  
  print_array(graph);
}