@candh/

UndirectedGraph[Matrix]

C

No description

fork
loading
Files
  • main.c

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.041160209560684224","path":"main.c","file":{"path":"main.c","content":{"asEncoding":{"base64":"I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnR5cGVkZWYgc3RydWN0IFVuZGlyZWN0ZWRHcmFwaCBVbmRpcmVjdGVkR3JhcGg7CnN0cnVjdCBVbmRpcmVjdGVkR3JhcGggewoJaW50IHZlcnRleGVzOwoJaW50ICoqIGFycmF5Owp9OwoKVW5kaXJlY3RlZEdyYXBoICogbWFrZV9ncmFwaChpbnQgdmVydGV4ZXMpIHsKCVVuZGlyZWN0ZWRHcmFwaCAqIGdyYXBoID0gKFVuZGlyZWN0ZWRHcmFwaCAqKSBtYWxsb2Moc2l6ZW9mKFVuZGlyZWN0ZWRHcmFwaCAqKSk7CgoJZ3JhcGgtPnZlcnRleGVzID0gdmVydGV4ZXM7CgoJLy8gbWFraW5nIGEgMkQgYXJyYXkKCWdyYXBoLT5hcnJheSA9IChpbnQgKiopIG1hbGxvYyh2ZXJ0ZXhlcyAqIHNpemVvZihpbnQgKikpOwoKCWludCBpLCBqOwoJZm9yIChpID0gMDsgaSA8IHZlcnRleGVzOyBpKyspIHsKCQlncmFwaC0+YXJyYXlbaV0gPSAoaW50ICopIG1hbGxvYyh2ZXJ0ZXhlcyAqIHNpemVvZihpbnQpKTsKCgkJLy8gaW5pdGlhbGl6aW5nIGV2ZXJ5IGVsZW1lbnQgd2l0aCAwCgkJZm9yIChqID0gMDsgaiA8IHZlcnRleGVzOyBqKyspIHsKCQkJZ3JhcGgtPmFycmF5W2ldW2pdID0gMDsKCQl9Cgl9CgoJcmV0dXJuIGdyYXBoOwp9Cgp2b2lkIGFkZF9lZGdlKFVuZGlyZWN0ZWRHcmFwaCAqIGdyYXBoLCBpbnQgdmVydGV4MSwgaW50IHZlcnRleDIpIHsKCWdy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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);
}