0
0
DSA Typescriptprogramming~20 mins

Graph vs Tree Key Structural Difference in DSA Typescript - Compare & Choose

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Graph vs Tree Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Key Structural Difference Between Graph and Tree
Which of the following statements correctly describes the key structural difference between a graph and a tree?
AA tree is a connected graph with no cycles, while a graph can have cycles and may be disconnected.
BA graph always has a root node, but a tree does not have a root.
CA tree can have cycles, but a graph cannot have cycles.
DA graph is always directed, while a tree is always undirected.
Attempts:
2 left
💡 Hint
Think about connectivity and cycles in both structures.
Predict Output
intermediate
1:30remaining
Output of Graph vs Tree Edge Count
Given a tree with 5 nodes, what is the number of edges it must have?
DSA Typescript
const nodes = 5;
const edgesInTree = nodes - 1;
console.log(edgesInTree);
A6
B4
C5
D3
Attempts:
2 left
💡 Hint
Remember the formula for edges in a tree.
Predict Output
advanced
2:30remaining
Detecting Cycle in Graph Code Output
What is the output of the following TypeScript code that checks for a cycle in an undirected graph?
DSA Typescript
class Graph {
  adjacencyList: Map<number, number[]> = new Map();

  addEdge(u: number, v: number) {
    if (!this.adjacencyList.has(u)) this.adjacencyList.set(u, []);
    if (!this.adjacencyList.has(v)) this.adjacencyList.set(v, []);
    this.adjacencyList.get(u)!.push(v);
    this.adjacencyList.get(v)!.push(u);
  }

  hasCycle(): boolean {
    const visited = new Set<number>();

    const dfs = (node: number, parent: number): boolean => {
      visited.add(node);
      for (const neighbor of this.adjacencyList.get(node) || []) {
        if (!visited.has(neighbor)) {
          if (dfs(neighbor, node)) return true;
        } else if (neighbor !== parent) {
          return true;
        }
      }
      return false;
    };

    for (const node of this.adjacencyList.keys()) {
      if (!visited.has(node)) {
        if (dfs(node, -1)) return true;
      }
    }
    return false;
  }
}

const g = new Graph();
g.addEdge(1, 2);
g.addEdge(2, 3);
g.addEdge(3, 4);
g.addEdge(4, 1);
console.log(g.hasCycle());
ASyntaxError
Bfalse
Cundefined
Dtrue
Attempts:
2 left
💡 Hint
Check if the edges form a cycle.
🧠 Conceptual
advanced
2:00remaining
Why Trees Cannot Have Cycles
Why is it impossible for a tree to have a cycle?
ABecause trees are defined as connected graphs with exactly one path between any two nodes.
BBecause trees have multiple paths between nodes.
CBecause trees allow cycles only if they are directed.
DBecause trees always have disconnected nodes.
Attempts:
2 left
💡 Hint
Think about the uniqueness of paths in a tree.
Predict Output
expert
2:30remaining
Output of Graph Connectivity Check
What is the output of this TypeScript code that checks if an undirected graph is connected?
DSA Typescript
class Graph {
  adjacencyList: Map<number, number[]> = new Map();

  addEdge(u: number, v: number) {
    if (!this.adjacencyList.has(u)) this.adjacencyList.set(u, []);
    if (!this.adjacencyList.has(v)) this.adjacencyList.set(v, []);
    this.adjacencyList.get(u)!.push(v);
    this.adjacencyList.get(v)!.push(u);
  }

  isConnected(): boolean {
    const visited = new Set<number>();
    const nodes = Array.from(this.adjacencyList.keys());
    if (nodes.length === 0) return true;

    const dfs = (node: number) => {
      visited.add(node);
      for (const neighbor of this.adjacencyList.get(node) || []) {
        if (!visited.has(neighbor)) dfs(neighbor);
      }
    };

    dfs(nodes[0]);
    return visited.size === nodes.length;
  }
}

const g = new Graph();
g.addEdge(1, 2);
g.addEdge(2, 3);
g.addEdge(4, 5);
console.log(g.isConnected());
ATypeError
Btrue
Cfalse
Dundefined
Attempts:
2 left
💡 Hint
Check if all nodes are reachable from the first node.