0
0
DSA Typescriptprogramming

Graph Terminology Vertices Edges Directed Undirected Weighted in DSA Typescript

Choose your learning style9 modes available
Mental Model
A graph is a way to connect points (vertices) with lines (edges) that can have directions and weights.
Analogy: Think of a city map where places are points and roads are lines connecting them. Some roads go one way (directed), some both ways (undirected), and some roads are longer or shorter (weighted).
Vertices: A, B, C, D
Edges:
A -> B (directed)
B ↔ C (undirected)
C -> D (directed, weight 5)

  A -> B
      ↘
       C -> D

Weights shown on edges if any
Dry Run Walkthrough
Input: Graph with vertices A, B, C, D; edges: A->B (directed), B-C (undirected), C->D (directed, weight 5)
Goal: Understand how vertices and edges connect, and how direction and weight affect the graph
Step 1: Identify vertices as points A, B, C, D
Vertices: [A] [B] [C] [D]
Why: Vertices are the main points or nodes in the graph
Step 2: Add directed edge from A to B
A -> B
Vertices: [A] [B] [C] [D]
Why: Directed edge means connection goes only from A to B
Step 3: Add undirected edge between B and C
A -> B ↔ C
Vertices: [A] [B] [C] [D]
Why: Undirected edge means connection goes both ways between B and C
Step 4: Add directed weighted edge from C to D with weight 5
A -> B ↔ C -> D (weight 5)
Vertices: [A] [B] [C] [D]
Why: Weight shows cost or distance on the edge from C to D
Result:
A -> B ↔ C -> D (weight 5)
Vertices: [A] [B] [C] [D]
Annotated Code
DSA Typescript
class Graph {
  vertices: Set<string>;
  edges: Map<string, Array<{to: string; directed: boolean; weight?: number}>>;

  constructor() {
    this.vertices = new Set();
    this.edges = new Map();
  }

  addVertex(v: string) {
    this.vertices.add(v);
    if (!this.edges.has(v)) {
      this.edges.set(v, []);
    }
  }

  addEdge(from: string, to: string, directed: boolean, weight?: number) {
    this.addVertex(from);
    this.addVertex(to);
    this.edges.get(from)!.push({ to, directed, weight });
    if (!directed) {
      this.edges.get(to)!.push({ to: from, directed, weight });
    }
  }

  printGraph() {
    for (const v of this.vertices) {
      const conns = this.edges.get(v)!.map(e => {
        const dir = e.directed ? '->' : '↔';
        const w = e.weight !== undefined ? ` (weight ${e.weight})` : '';
        return `${dir} ${e.to}${w}`;
      });
      console.log(`${v}: ${conns.join(', ')}`);
    }
  }
}

// Driver code
const graph = new Graph();
graph.addEdge('A', 'B', true); // directed A->B
graph.addEdge('B', 'C', false); // undirected B<->C
graph.addEdge('C', 'D', true, 5); // directed C->D with weight 5

graph.printGraph();
this.edges.get(from)!.push({ to, directed, weight });
Add edge from 'from' vertex to 'to' vertex with direction and optional weight
if (!directed) { this.edges.get(to)!.push({ to: from, directed, weight }); }
If edge is undirected, add reverse edge from 'to' to 'from'
for (const v of this.vertices) { const conns = this.edges.get(v)!.map(e => { const dir = e.directed ? '->' : '↔'; const w = e.weight !== undefined ? ` (weight ${e.weight})` : ''; return `${dir} ${e.to}${w}`; }); console.log(`${v}: ${conns.join(', ')}`); }
Print each vertex and its connected edges with direction and weight
OutputSuccess
A: -> B B: ↔ C C: ↔ B, -> D (weight 5) D:
Complexity Analysis
Time: O(V + E) because we add vertices and edges once and print all connections
Space: O(V + E) because we store all vertices and edges in memory
vs Alternative: Compared to adjacency matrix (O(V^2) space), adjacency list used here is more space efficient for sparse graphs
Edge Cases
Adding edge where vertices do not exist yet
Vertices are automatically added before adding edges
DSA Typescript
this.addVertex(from);
Adding undirected edge
Edge is added in both directions to represent two-way connection
DSA Typescript
if (!directed) { this.edges.get(to)!.push({ to: from, directed, weight }); }
Adding weighted edge
Weight is stored and printed alongside the edge
DSA Typescript
const w = e.weight !== undefined ? ` (weight ${e.weight})` : '';
When to Use This Pattern
When a problem involves connecting points with lines that may have directions or costs, think of graph terminology to model vertices, edges, direction, and weights.
Common Mistakes
Mistake: Forgetting to add the reverse edge for undirected edges
Fix: Add the reverse edge explicitly when the edge is undirected
Mistake: Not adding vertices before adding edges causing errors
Fix: Always add vertices first or ensure they exist before adding edges
Summary
Graphs connect points called vertices with lines called edges that can be directed or undirected and may have weights.
Use graph terminology when modeling relationships with direction or cost between items.
Remember edges can go one way (directed) or both ways (undirected), and weights show cost or distance.