Open ak2253 opened 4 years ago
Sorry, I don't understand your way, especially the "loop through the set". Beside it, I don't think I need to combine the two for loop here when it can work and by the way, combine them cannot reduce the time complexity.
Hello, the method that I recommend does not change the overall time complexity (still O(n^2)). It was a recommendation to shorten you overall code without increasing the time complexity. If you take wg.addNode(i); from line 117 and place it in between line 120 and 121, and you change the nested for loop in line 121 into for(Node node:wg graph), you can delete lines 116-118 and reach the same results (you may need to check for duplicate nodes depending on how you change it).
If its too uncomfortable to combine the 2 for loops especially if my explanation only confused you even more, do not change it since it only affects readability number of lines to read.
You can combine the 2 for loops on line 116 and line 120 in the following method: https://github.com/NeverFlight/CS435-Project2-Part2/blob/d2214e6197e00763e03a18aacb510f210ff396cb/src/com/cs435/part2/Main.java#L113-L132 You can do that by initializing the first node into the graph. Then for the rest of n-1 nodes, initialize it and loop through the set where all of the previously made nodes are stored and connect them. After that, store the initialized node into the set.