Skip to content

Latest commit

 

History

History
490 lines (430 loc) · 12.5 KB

DAA_DSA_EXACT_MATCH.md

File metadata and controls

490 lines (430 loc) · 12.5 KB

DAA ↔ DSA Dijkstra - EXACT Structure Match! ✅

Critical Fix: Weight Box Positioning

The Problem:

DAA was using getEdgeLabelX() and getEdgeLabelY() methods that added extra offsets, causing weight boxes to be misaligned.

DSA Approach (Inline Calculation):

<!-- Background rect -->
<rect
  [attr.x]="(nodes[e.from].x + nodes[e.to].x) / 2 - 18"
  [attr.y]="(nodes[e.from].y + nodes[e.to].y) / 2 - 23"
  width="36"
  height="26"
  rx="6"
/>
<!-- Weight label -->
<text 
  [attr.x]="(nodes[e.from].x + nodes[e.to].x) / 2" 
  [attr.y]="(nodes[e.from].y + nodes[e.to].y) / 2 - 5"
>
  {{ weight }}
</text>

DAA Old Approach (Method-based - WRONG):

<!-- Background rect -->
<rect
  [attr.x]="getEdgeLabelX(e) - 18"  ← getEdgeLabelX returns (x1+x2)/2
  [attr.y]="getEdgeLabelY(e) - 23"  ← getEdgeLabelY returns (y1+y2)/2 - 5
/>
<!-- Weight label -->
<text 
  [attr.x]="getEdgeLabelX(e)"       ← (x1+x2)/2 ✓
  [attr.y]="getEdgeLabelY(e)"       ← (y1+y2)/2 - 5 ✓
>

Result:

  • Text Y position: (y1+y2)/2 - 5 ✅
  • Rect Y position: ((y1+y2)/2 - 5) - 23 = (y1+y2)/2 - 28 ❌ 5px too high!

DAA New Approach (Inline - CORRECT):

<!-- Background rect -->
<rect
  [attr.x]="(nodes[e.from].x + nodes[e.to].x) / 2 - 18"
  [attr.y]="(nodes[e.from].y + nodes[e.to].y) / 2 - 23"
  width="36"
  height="26"
  rx="6"
/>
<!-- Weight label -->
<text 
  [attr.x]="(nodes[e.from].x + nodes[e.to].x) / 2" 
  [attr.y]="(nodes[e.from].y + nodes[e.to].y) / 2 - 5"
>
  {{e.w}}
</text>

Result:

  • Text Y position: (y1+y2)/2 - 5 ✅
  • Rect Y position: (y1+y2)/2 - 23 ✅
  • Perfect alignment like DSA!

Complete Structure Comparison

1. SVG Container Structure

DSA:

<svg viewBox="0 0 800 500">
  <defs>
    <marker id="arrow">...</marker>
  </defs>
  
  <!-- Draw edges -->
  <g class="edges">
    <g *ngFor="let edge of edges">
      <line />
      <rect class="weight-bg" />
      <text class="edge-weight" />
    </g>
  </g>
  
  <!-- Draw nodes -->
  <g class="nodes">
    <g *ngFor="let node of nodes">
      <circle class="exploring-ring" />
      <circle class="node-circle" />
      <text class="node-label" />
      <text class="distance-label" />
    </g>
  </g>
</svg>

DAA (NOW MATCHES):

<svg viewBox="0 0 800 500">
  <defs>
    <marker id="arrow">...</marker>
  </defs>
  
  <!-- Draw edges - EXACT DSA STRUCTURE -->
  <g class="edges">
    <g *ngFor="let e of edges">
      <line />
      <rect class="weight-bg" />
      <text class="edge-weight" />
    </g>
  </g>
  
  <!-- Draw nodes - EXACT DSA STRUCTURE -->
  <g class="nodes">
    <g *ngFor="let idx of displayOrder">
      <circle class="exploring-ring" />
      <circle class="node-circle" />
      <text class="node-label" />
      <text class="distance-label" />
    </g>
  </g>
</svg>

2. Edge Rendering

DSA:

<g *ngFor="let edge of edges">
  <!-- Edge line -->
  <line 
    [attr.x1]="getNode(edge.from).x" 
    [attr.y1]="getNode(edge.from).y"
    [attr.x2]="getNode(edge.to).x" 
    [attr.y2]="getNode(edge.to).y"
    [attr.class]="getEdgeLineClass(edge)"
  />
  <!-- Weight box -->
  <rect
    [attr.x]="(getNode(edge.from).x + getNode(edge.to).x) / 2 - 18"
    [attr.y]="(getNode(edge.from).y + getNode(edge.to).y) / 2 - 23"
    width="36"
    height="26"
    rx="6"
    [ngClass]="{'weight-bg': true, 'weight-bg-active': isEdgeActive(edge)}"
  />
  <!-- Weight text -->
  <text 
    [attr.x]="(getNode(edge.from).x + getNode(edge.to).x) / 2" 
    [attr.y]="(getNode(edge.from).y + getNode(edge.to).y) / 2 - 5"
    [attr.class]="isEdgeActive(edge) ? 'edge-weight active' : 'edge-weight'"
  >
    {{ edge.weight }}
  </text>
</g>

DAA (NOW MATCHES):

<g *ngFor="let e of edges">
  <!-- Edge line -->
  <line 
    [attr.x1]="nodes[e.from].x" 
    [attr.y1]="nodes[e.from].y" 
    [attr.x2]="nodes[e.to].x" 
    [attr.y2]="nodes[e.to].y"
    [ngClass]="{
      'edge-tree': prev[e.to] === e.from,
      'edge-active': activeEdge && activeEdge.from === e.from && activeEdge.to === e.to
    }"
  />
  <!-- Weight box - EXACT DSA positioning -->
  <rect
    [attr.x]="(nodes[e.from].x + nodes[e.to].x) / 2 - 18"
    [attr.y]="(nodes[e.from].y + nodes[e.to].y) / 2 - 23"
    width="36"
    height="26"
    rx="6"
    [ngClass]="{
      'weight-bg': true,
      'weight-bg-active': activeEdge && activeEdge.from === e.from && activeEdge.to === e.to
    }"
  />
  <!-- Weight text - EXACT DSA positioning -->
  <text 
    [attr.x]="(nodes[e.from].x + nodes[e.to].x) / 2" 
    [attr.y]="(nodes[e.from].y + nodes[e.to].y) / 2 - 5"
    [attr.class]="(activeEdge && activeEdge.from === e.from && activeEdge.to === e.to) ? 'edge-weight active' : 'edge-weight'"
  >
    {{e.w}}
  </text>
</g>

Key Match:

  • ✅ Same inline calculation for X and Y
  • ✅ Same offsets (-18 for X, -23 for Y on rect)
  • ✅ Same text positioning (midpoint X, midpoint - 5 for Y)
  • ✅ Same conditional classes

3. Node Rendering

DSA:

<g *ngFor="let node of nodes">
  <!-- Exploring ring -->
  <circle 
    *ngIf="node.id === currentNode"
    [attr.cx]="node.x" 
    [attr.cy]="node.y" 
    [attr.r]="38"
    class="exploring-ring"
  />
  <!-- Node circle -->
  <circle 
    [attr.cx]="node.x" 
    [attr.cy]="node.y" 
    [attr.r]="node.id === currentNode ? 30 : 25"
    [attr.class]="getNodeCircleClass(node)"
  />
  <!-- Node label (inside) -->
  <text 
    [attr.x]="node.x" 
    [attr.y]="node.y + 5" 
    class="node-label"
  >
    {{ node.id }}
  </text>
  <!-- Distance (outside, below) -->
  <text 
    [attr.x]="node.x" 
    [attr.y]="node.y + (node.id === currentNode ? 30 : 25) + 18" 
    class="distance-label"
  >
    {{ node.distance === Infinity ? '∞' : node.distance }}
  </text>
</g>

DAA (NOW MATCHES):

<g *ngFor="let idx of displayOrder">
  <!-- Exploring ring -->
  <circle 
    *ngIf="activeNode === idx"
    [attr.cx]="nodes[idx].x" 
    [attr.cy]="nodes[idx].y" 
    [attr.r]="38"
    class="exploring-ring"
  />
  <!-- Node circle -->
  <circle 
    [attr.cx]="nodes[idx].x" 
    [attr.cy]="nodes[idx].y" 
    [attr.r]="activeNode === idx ? 30 : 25"
    [attr.class]="getNodeCircleClass(idx)"
  />
  <!-- Node label (inside) -->
  <text 
    [attr.x]="nodes[idx].x" 
    [attr.y]="nodes[idx].y + 5" 
    class="node-label">
    {{displayIndex[idx]}}
  </text>
  <!-- Distance (outside, below) -->
  <text 
    [attr.x]="nodes[idx].x" 
    [attr.y]="nodes[idx].y + (activeNode === idx ? 30 : 25) + 18" 
    class="distance-label"
  >
    {{dist[idx] === INF ? '∞' : dist[idx]}}
  </text>
</g>

Key Match:

  • ✅ Same structure (ring, circle, label, distance)
  • ✅ Same conditional logic (activeNode vs currentNode)
  • ✅ Same sizing (radius 38 ring, 30/25 circle)
  • ✅ Same text positioning (+5 inside, +radius+18 outside)

4. CSS Styling

Edge Classes (MATCH):

/* Base line style */
.graph-svg line { 
  stroke: #4a5568;
  stroke-width: 2;
}

/* Active edge (being checked) */
line.edge-active { 
  stroke: #fbbf24;
  stroke-width: 4;
  animation: edgePulse 0.8s ease-in-out infinite;
}

/* Tree edge (in shortest path) */
line.edge-tree { 
  stroke: #10b981;
  stroke-width: 3;
}

/* Weight box */
.weight-bg {
  fill: rgba(15, 23, 42, 0.95);
  stroke: #475569;
  stroke-width: 1.5;
}

/* Active weight box */
.weight-bg-active {
  fill: rgba(251, 191, 36, 0.3);
  stroke: #fbbf24;
  stroke-width: 2.5;
  filter: drop-shadow(0 0 12px rgba(251, 191, 36, 0.6));
  animation: weightPulse 1s ease-in-out infinite;
}

Node Classes (MATCH):

/* Base node */
.node-circle {
  stroke-width: 3;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Unvisited */
.node-circle.unvisited {
  fill: #041014;
  stroke: #4a5568;
}

/* In queue (source) - NO PULSE */
.node-circle.in-queue {
  fill: #1e40af;
  stroke: #3b82f6;
  filter: drop-shadow(0 0 8px rgba(59,130,246,0.7));
}

/* Exploring (active) */
.node-circle.exploring {
  fill: #fbbf24;
  stroke: #f59e0b;
  stroke-width: 4;
  filter: drop-shadow(0 0 18px #fbbf24);
  animation: softGlow 0.7s ease-in-out infinite alternate;
}

/* Visited */
.node-circle.visited {
  fill: #10b981;
  stroke: #059669;
  filter: drop-shadow(0 0 10px #10b981);
}

/* Exploring ring */
.exploring-ring {
  fill: none;
  stroke: #fbbf24;
  stroke-width: 2;
  stroke-dasharray: 10 5;
  animation: ringPulse 2s linear infinite;
}

Visual Result Comparison

DSA Dijkstra:

[Graph centered]
Nodes: Blue (source) → Yellow (exploring) → Green (visited)
Edges: Gray → Yellow (active) → Green (tree)
Weights: Dark boxes → Yellow glowing boxes
Distance: Below nodes, adapts to size

DAA Dijkstra (NOW MATCHES):

[Graph left, Table right]
Nodes: Blue (source, static) → Yellow (exploring) → Green (visited)
Edges: Gray → Yellow (active) → Green (tree)
Weights: Dark boxes → Yellow glowing boxes ✅ FIXED!
Distance: Below nodes, adapts to size ✅

What Changed in This Fix

Files Modified:

e:\Algorithm visualizer\algorithm-visualizer\frontend\src\app\pages\daa\dijkstra.component.ts

Changes:

  1. Edge rendering (Lines ~45-75):

    • ❌ OLD: Used getEdgeLabelX(e) and getEdgeLabelY(e) methods
    • ✅ NEW: Uses inline (nodes[e.from].x + nodes[e.to].x) / 2 calculation
    • Result: Weight boxes now perfectly centered like DSA
  2. Node rendering (Lines ~77-110):

    • ✅ Added <g class="nodes"> wrapper for consistency
    • ✅ Changed node label Y from +4 to +5 (matches DSA)
    • Result: Better vertical alignment
  3. CSS (Line ~218):

    • ❌ OLD: .node-circle.in-queue had animation: softPulse
    • ✅ NEW: .node-circle.in-queue has static filter: drop-shadow
    • Result: Source node doesn't pulse

Testing Checklist

✅ Weight Box Position:

  1. Hard refresh: Ctrl + Shift + R
  2. Check weight labels (7, 3, 4, etc.)
  3. Should be centered in dark rounded boxes
  4. Numbers should be vertically centered in boxes
  5. Boxes should not be offset up/down

✅ Node Animations:

  1. Node A starts blue (static, no pulse)
  2. Click Play
  3. Node A turns yellow with ring
  4. Distance "0" stays below node
  5. Node A turns green when done

✅ Edge Animations:

  1. Edges start gray
  2. Active edge turns yellow with pulse
  3. Weight box glows yellow
  4. Tree edges turn green
  5. Weight boxes return to dark

✅ Overall Structure:

  1. Graph on left (800x500 viewport)
  2. Table on right (320px wide)
  3. Controls at bottom
  4. Everything properly aligned

Expected Visual Result

Perfect DSA-quality animations with proper weight box positioning!

Initial State:
┌─────────────────────────────┬──────────────┐
│         🔵 A (0)            │  Node | Dist │
│       /   |   \             │  ─────┼───── │
│     [7] [3]  [4]            │   A   │  0   │
│     /    |    \             │   B   │  ∞   │
│   🔴 B  🔴 C  🔴 D          │   C   │  ∞   │
│  (∞)   (∞)   (∞)            │   ...        │
└─────────────────────────────┴──────────────┘

After Play (Processing A):
┌─────────────────────────────┬──────────────┐
│         🟡 A (0)⭕          │  Node | Dist │
│       / ⚡ | ⚡ \           │  ─────┼───── │
│     [7] [3]  [4] ← Glowing  │   A   │  0   │
│     /    |    \             │   B   │  4   │
│   🔴 B  🔴 C  🔴 D          │   C   │  2   │
│  (4)   (2)   (9)            │   ...        │
└─────────────────────────────┴──────────────┘

Final State:
┌─────────────────────────────┬──────────────┐
│         🟢 A (0)            │  Node | Dist │
│       /   |   \             │  ─────┼───── │
│     [7] [3]  [4]            │   A   │  0   │
│     /    |    \             │   B   │  4   │
│   🟢 B  🟢 C  🟢 D          │   C   │  2   │
│  (4)   (2)   (9)            │   D   │  9   │
└─────────────────────────────┴──────────────┘

All weight boxes properly aligned, no offset issues! 🎉