Complete the code to check if a graph has cycles.
bool HasCycle = graph.[1]();The method HasCycle checks if the graph contains any cycles, which is a key difference from trees.
Complete the code to add an edge in an undirected graph.
graph.AddEdge([1], [2]);
Adding an edge requires two nodes: node1 and node2. The order does not matter for undirected graphs as edges are bidirectional.
Fix the error in the code that checks if a tree is connected.
bool isConnected = tree.[1]();The IsConnected method checks if all nodes in the tree are reachable, which is essential for trees.
Fill both blanks to create a tree from a graph by removing cycles.
while(graph.[1]()) { graph.[2](); }
We check if the graph has cycles using HasCycle, and remove edges causing cycles with RemoveCycleEdge to form a tree.
Fill all three blanks to verify if a graph is a tree.
bool isTree = graph.[1]() && !graph.[2]() && graph.[3]();
A graph is a tree if it is connected (IsConnected), has no cycles (HasCycle returns false), and has a single root (HasSingleRoot).
