0
0
DSA Typescriptprogramming~20 mins

Graph Terminology Vertices Edges Directed Undirected Weighted in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Graph Terminology Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of adjacency list for directed weighted graph
What is the printed adjacency list after running this TypeScript code for a directed weighted graph?
DSA Typescript
type Edge = { to: number; weight: number };
const graph: Map<number, Edge[]> = new Map();

function addEdge(from: number, to: number, weight: number) {
  if (!graph.has(from)) graph.set(from, []);
  graph.get(from)!.push({ to, weight });
}

addEdge(1, 2, 5);
addEdge(1, 3, 10);
addEdge(2, 3, 2);
addEdge(3, 1, 7);

for (const [node, edges] of graph.entries()) {
  console.log(`${node} -> ${edges.map(e => `${e.to}(${e.weight})`).join(', ')}`);
}
A1 -> 2(5), 3(10)\n2 -> 3(2)\n3 -> 1(0)
B1 -> 2(5), 3(10)\n2 -> 3(2)\n3 -> 1(7)
C1 -> 2(5)\n2 -> 3(2), 1(7)\n3 -> 3(10)
D1 -> 3(10)\n2 -> 1(5), 3(2)\n3 -> 1(7)
Attempts:
2 left
💡 Hint
Check how edges are added and printed for each node in the directed graph.
🧠 Conceptual
intermediate
1:30remaining
Identify the type of graph from description
Which type of graph is described by: "Edges have no direction and each edge has a number representing cost or distance"?
ADirected Weighted Graph
BDirected Unweighted Graph
CUndirected Weighted Graph
DUndirected Unweighted Graph
Attempts:
2 left
💡 Hint
Edges without direction mean connections go both ways.
🔧 Debug
advanced
2:00remaining
Find the error in this undirected graph edge addition
What error will occur when running this TypeScript code to add edges in an undirected graph?
DSA Typescript
const graph: Map<number, number[]> = new Map();

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

addEdge(1, 2);
addEdge(2, 3);
console.log(graph);
ATypeError: Cannot read property 'push' of undefined
BNo error, graph prints correctly
CReferenceError: graph is not defined
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint
Check if both nodes have arrays initialized before pushing.
Predict Output
advanced
2:00remaining
Output of BFS traversal on undirected graph
What is the order of nodes printed by this BFS traversal on the undirected graph?
DSA Typescript
const graph: Map<number, number[]> = new Map();

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

addEdge(1, 2);
addEdge(1, 3);
addEdge(2, 4);
addEdge(3, 5);

function bfs(start: number) {
  const visited = new Set<number>();
  const queue: number[] = [];
  queue.push(start);
  visited.add(start);
  const order: number[] = [];

  while (queue.length > 0) {
    const node = queue.shift()!;
    order.push(node);
    for (const neighbor of graph.get(node)!) {
      if (!visited.has(neighbor)) {
        visited.add(neighbor);
        queue.push(neighbor);
      }
    }
  }
  return order;
}

console.log(bfs(1).join(' -> '));
A1 -> 2 -> 3 -> 4 -> 5
B1 -> 3 -> 2 -> 5 -> 4
C1 -> 2 -> 4 -> 3 -> 5
D1 -> 3 -> 5 -> 2 -> 4
Attempts:
2 left
💡 Hint
BFS visits neighbors level by level in the order they appear in adjacency list.
🧠 Conceptual
expert
1:30remaining
Which graph type allows cycles and weighted edges but no direction?
Select the graph type that can have cycles, edges with weights, and edges without direction.
ADirected Acyclic Weighted Graph
BUndirected Unweighted Graph without cycles
CDirected Weighted Graph without cycles
DUndirected Weighted Graph
Attempts:
2 left
💡 Hint
Edges without direction and with weights allow cycles.