Routing Algorithms

The brain of the Network Layer: How routers decide the best path for your data.

Forwarding vs. Routing

A router has two primary jobs that work at different scales and speeds.

Forwarding

  • Action: Move packet from input link to output link.
  • Scale: Purely local computation.
  • Speed: Must be very fast (nanoseconds).
التمرير (Forwarding): عملية محلية وسريعة جداً، الراوتر بياخد الباكت من بورت الدخول يوديه لبورت الخروج.

Routing

  • Action: Determine the end-to-end path.
  • Scale: Global decisions (Network-wide).
  • Speed: Slower (important when topology changes).
التوجيه (Routing): عملية عالمية بتحسب أحسن طريق من المصدر للهدف، وبتحتاج وقت أكتر.
Routing as a Graph Problem

To solve routing, we model the network as a Graph (G = V, E).

  • Nodes (V): Represent Routers.
  • Edges (E): Represent Physical Links.
  • Cost (c): Delay, $, or congestion level.
Goal: Find a "good" path (minimum cost).
A B C D cost=2 cost=5 cost=1 cost=3
بنمثل الشبكة كأنها خريطة (Graph). الدوائر هي الراوترات، والخطوط هي الكابلات. كل خط عليه "تكلفة" (Cost) ممكن تكون وقت أو فلوس، وهدفنا نلاقي أرخص طريق.
Link State: Dijkstra's Algorithm

Link State algorithms require every router to know the entire network topology via "Link State Broadcast".

Key Notation:

  • c(i,j): Cost from node i to j.
  • D(v): Current cost of path to destination v.
  • p(v): Predecessor (the node before v).
  • N: Set of nodes whose least cost is known.

Algorithm Steps (Simplified)

  1. Initial: Start with the source. Neighbors get their actual cost, others get ∞ (infinity).
  2. Find Minimum: Look for the node w not in N with the smallest D(w).
  3. Update: For all neighbors of w, update their costs if going through w is cheaper.
  4. Repeat: Until all nodes are in N.
خوارزمية ديكسترا (Dijkstra) بتخلي كل راوتر يحسب أقصر طريق لكل الراوترات التانية بناءً على "خريطة كاملة" عنده للشبكة. بتشتغل بنظام التكرار لحد ما تضمن إنها لقت أحسن طريق لكل نقطة.
Distance Vector Algorithm

Unlike Dijkstra, this is Distributed and Iterative. Routers only talk to their neighbors.

Bellman-Ford Equation (The Logic):

DX(Y,Z) = c(X,Z) + minw { DZ(Y,w) }

Cost from X to Y via neighbor Z = (Cost to Z) + (Best cost from Z to Y).

Key Characteristics:

  • Iterative: Self-terminating (stops when no more info).
  • Asynchronous: Neighbors don't need to be in lock-step.
  • Distributed: Each node calculates its own table.

شرح الـ Distance Vector:

الراوتر هنا "عمي" شوية، مش شايف الخريطة كلها. هو بس بيسأل جيرانه: "إنتو بتوصلوا لـ (أ) في قد إيه؟". وبناءً على ردهم بيحسب أحسن طريق لنفسه. ميزته إنه بسيط وبيستجيب للتغييرات بسرعة بين الجيران.

Practice Arena 🎯

1. Dijkstra's algorithm is the core of which type of routing approach?