I am given the input in the following manner, where the first line contains the number of vertices and edges of the undirected graph; and the lines to follow contain the names of vertices between which there is an edge.
Input:
9 8
sainteanne fortdefrance
bassepointe latrinite
bridgetown jackmans
fortdefrance ducos
holetown bridgetown
shanty bridgetown
fortdefrance bassepointe
jackmans shanty
This means that the graph has 9 vertices and 8 edges. There exist edges between the elements in each of the above mentioned pairs. The end goal is to find connected components in this graph.
However, working with vertices as index numbers is easier than working with strings. Therefore, I am looking for a way to convert the above information into something this:
0 1
2 3
4 5
1 6
7 4
8 4
1 2
5 8
I have written the following C code to read the file and create a structure containing edges in which the vertex id must be stored.
typedef struct {
unsigned int first;
unsigned int second;
} edge;
int main()
{
unsigned int order; // no.of Vertices
unsigned int n; // no.of Edges
edge *edges;
scanf("%u %u",&order,&n);
edges = malloc(n * sizeof(edge));
char first_string[20];
char second_string[20];
for (int i=0;i<n;i++)
{
scanf("%s %s",first_string,second_string);
}
}