Which of the following is a key reason why trees cannot model all types of relationships that graphs can?
Think about what a tree structure looks like compared to a graph.
Trees are hierarchical and cannot have cycles or nodes with multiple parents, while graphs can represent cycles and complex connections.
What is the output of the following Python code that tries to represent a cycle using adjacency lists?
graph = { 'A': ['B'], 'B': ['C'], 'C': ['A'] }
print(graph)Look at how adjacency lists represent edges.
The dictionary shows each node with its connected nodes, including the cycle from C back to A.
What is the output of the following Python code that tries to detect a cycle in a graph represented as adjacency list?
def has_cycle(graph, node, visited, parent): visited.add(node) for neighbor in graph[node]: if neighbor not in visited: if has_cycle(graph, neighbor, visited, node): return True elif neighbor != parent: return True return False graph = {'A': ['B'], 'B': ['C'], 'C': ['A']} visited = set() print(has_cycle(graph, 'A', visited, None))
Trace the recursive calls and check the cycle detection logic.
The function correctly detects the cycle A->B->C->A and returns True.
What is the output of this adjacency list representing a graph where a node has multiple parents?
graph = {'A': ['C'], 'B': ['C'], 'C': []}
print(graph)Check how edges are represented from parents to children.
The graph shows nodes A and B both pointing to C, meaning C has multiple parents.
Which real-life scenario best illustrates why graphs are necessary and trees are insufficient?
Think about relationships that are not strictly hierarchical.
Social networks have complex, mutual, and multiple connections that trees cannot represent, but graphs can.
