I want to create an adjacence matrix given n lines that represent a partial connection between some nodes of a graph. For example, due to the fact that each line represent a clique, the lines A-B; B-C; C-D; A-E-D
form the following graph.
My first approach was using a for loop
to read each line, for each line I use another for loop
to obtain each node in it, finally, with another for loop
I check if the rest of the nodes are already in the adyacence list of the node analized, if not, I add it. All of this gives a O(n^3) complexity. Is there another way for making this task with a lower complexity? Is it possible to complete this with O(n)?