Discover how graphs turn messy connections into clear maps you can explore and understand!
Why Graph Terminology Vertices Edges Directed Undirected Weighted in DSA Typescript?
Imagine you want to map all your friends and how they are connected to each other. You try to write down every friendship and direction manually on paper.
Writing and updating connections by hand is slow and confusing. You might forget who is connected to whom or miss if the connection has a direction or weight, like how close the friendship is.
Graphs let you organize all these connections clearly. You can represent friends as points (vertices) and friendships as lines (edges). You can also show if friendships go one way (directed) or both ways (undirected), and add importance (weights) easily.
const friends = ['Alice', 'Bob', 'Carol']; // Manually track connections with notes // Alice -> Bob (close), Bob <-> Carol (friends)
class Graph { vertices: string[] = []; edges: Map<string, Map<string, number>> = new Map(); // Add vertices and edges with direction and weight }
Graphs let you quickly find relationships, paths, and strengths between points in complex networks.
Social media platforms use graphs to show who follows whom, suggest friends, and rank connections by interaction strength.
Vertices are points or nodes representing entities.
Edges connect vertices and can have direction and weight.
Graphs help model complex relationships clearly and efficiently.
Practice
Solution
Step 1: Understand graph components
A graph consists of points called nodes and connections called edges.Step 2: Identify the term for nodes
The points or nodes where edges meet are called vertices.Final Answer:
Vertices -> Option CQuick Check:
Nodes = Vertices [OK]
- Confusing edges with vertices
- Thinking weights are nodes
- Mixing paths with nodes
Solution
Step 1: Understand object syntax in TypeScript
Objects use key-value pairs with curly braces and colons.Step 2: Check each option's syntax
const edge = { from: 1, to: 2, weight: 5 }; correctly uses keys 'from', 'to', and 'weight' with values. const edge = [1, 2, 5]; is an array, valid but less descriptive. const edge = (1, 2, 5); uses parentheses, invalid for object. const edge = { 1: 2, 5 }; has invalid syntax.Final Answer:
const edge = { from: 1, to: 2, weight: 5 }; -> Option DQuick Check:
Object with keys and values = const edge = { from: 1, to: 2, weight: 5 }; [OK]
- Using parentheses instead of braces
- Incorrect object key syntax
- Confusing arrays with objects
type Edge = { from: number; to: number; weight: number };
const graph: Edge[] = [
{ from: 1, to: 2, weight: 3 },
{ from: 2, to: 3, weight: 4 },
{ from: 3, to: 1, weight: 5 }
];
for (const edge of graph) {
console.log(`${edge.from} -> ${edge.to} (weight: ${edge.weight})`);
}Solution
Step 1: Read the graph array
The graph array has edges: 1->2 with weight 3, 2->3 with weight 4, 3->1 with weight 5.Step 2: Trace the for loop output
The loop prints each edge in the format 'from -> to (weight: weight)'. So output matches 1 -> 2 (weight: 3) 2 -> 3 (weight: 4) 3 -> 1 (weight: 5) exactly.Final Answer:
1 -> 2 (weight: 3) 2 -> 3 (weight: 4) 3 -> 1 (weight: 5) -> Option AQuick Check:
Print edges as defined = 1 -> 2 (weight: 3) 2 -> 3 (weight: 4) 3 -> 1 (weight: 5) [OK]
- Mixing up weights and vertices
- Reversing edge directions
- Assuming syntax error
type Edge = { from: number; to: number; weight: number };
const graph: Edge[] = [
{ from: 1, to: 2, weight: 7 },
{ from: 2, to: 1, weight: 7 },
{ from: 2, to: 3, weight: 5 }
];
function addEdge(from: number, to: number, weight: number) {
graph.push({ from, to, weight });
graph.push({ from: to, to: from, weight });
}
addEdge(3, 4, 6);
console.log(graph.length);Solution
Step 1: Count initial edges
Initially, graph has 3 edges: 1->2, 2->1, 2->3.Step 2: Analyze addEdge function call
addEdge(3, 4, 6) pushes { from: 3, to: 4, weight: 6 } and { from: 4, to: 3, weight: 6 }, adding 2 more edges for undirected graph.Step 3: Calculate final length
3 initial + 2 added = 5. No errors occur; code runs successfully.Final Answer:
No error; graph length will be 5 -> Option AQuick Check:
3 + 2 = 5 [OK]
- Assuming duplicate edges cause error
- Miscounting edges after push
- Thinking graph is immutable
const graph: Record = {
1: [{ node: 2, weight: 4 }, { node: 3, weight: 1 }],
2: [{ node: 1, weight: 4 }, { node: 3, weight: 2 }, { node: 4, weight: 5 }],
3: [{ node: 1, weight: 1 }, { node: 2, weight: 2 }, { node: 4, weight: 8 }],
4: [{ node: 2, weight: 5 }, { node: 3, weight: 8 }]
};
// How to find the total weight of edges connected to vertex 2?
Which code snippet correctly calculates the total weight of edges connected to vertex 2?Solution
Step 1: Understand adjacency list structure
graph[2] is an array of edges connected to vertex 2, each with a weight property.Step 2: Calculate total weight
Use reduce to sum all edge weights in graph[2]. const total = graph[2].reduce((sum, edge) => sum + edge.weight, 0); correctly sums weights. const total = graph[2].map(edge => edge.weight).length; counts edges, not sum. const total = graph[2].filter(edge => edge.weight > 0); filters edges, no sum. const total = graph.reduce((sum, edges) => sum + edges.weight, 0); tries to reduce graph object, invalid.Final Answer:
const total = graph[2].reduce((sum, edge) => sum + edge.weight, 0); -> Option BQuick Check:
Sum weights with reduce = const total = graph[2].reduce((sum, edge) => sum + edge.weight, 0); [OK]
- Using map length instead of sum
- Filtering without summing
- Reducing wrong data structure
Ready to prove it?
Pass the test and this pattern moves unknown
