Skip to content

Instantly share code, notes, and snippets.

@padawin
Created November 9, 2017 15:30
Show Gist options
  • Save padawin/5f16e94f8531157fab3c42629524d071 to your computer and use it in GitHub Desktop.
Save padawin/5f16e94f8531157fab3c42629524d071 to your computer and use it in GitHub Desktop.
bidirectional graph structure
#include<stdio.h>
#include<inttypes.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define SIZE 4
/*
* Gets Row from min(), (diff of x and y) +
*/
int idx(size_t number_nodes, int node1, int node2) {
int i = (node1 < node2) ? node1 : node2;
return abs(node1 - node2) + (number_nodes * i) - ((i * (i - 1)) / 2);
}
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("2 nodes are required\n");
return 1;
}
int node1 = argv[1][0] - 'A';
int node2 = argv[2][0] - 'A';
printf("Provided: %c (%d), %c (%d)\n", argv[1][0], node1, argv[2][0], node2);
if (node1 >= SIZE || node2 >= SIZE || node1 < 0 || node2 < 0) {
printf("Invalid nodes, must be between 0 and %d\n", SIZE - 1);
return 1;
}
uint32_t *first_array = malloc(sizeof(uint32_t) * SIZE * (SIZE + 1) / 2);
/* A B C D
* A 0 1 12 0
* B 1 0 2 4
* C 12 2 0 8
* D 0 4 8 0
*/
first_array[0] = 0;
first_array[1] = 1;
first_array[2] = 12;
first_array[3] = 0;
first_array[4] = 0;
first_array[5] = 2;
first_array[6] = 4;
first_array[7] = 0;
first_array[8] = 8;
first_array[9] = 0;
int t1 = idx(SIZE, node1, node2);
int t2 = idx(SIZE, node2, node1);
printf("T1: %i\nT2: %i\n", t1, t2);
printf("V1: %i\nV2: %i\n", first_array[t1], first_array[t2]);
free(first_array);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment