White-Path Theorem

The white-path theorem states that if there is a path between two vertices consisting of white-colored edges, then there must exist a shortest path that is also white between those vertices.

This has applications in analyzing shortest paths with categorizations of edges.

For example, in a road network:

  • White edges = highways
  • Black edges = local roads

Then there exists a shortest highway-only path between cities if any path exists using highways.

Java - Road network example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
boolean hasAllHighwayPath(Graph graph, int src, int dest) {
  Queue<Integer> q = new LinkedList<>();
  q.add(src); 
  Set<Integer> visited = new HashSet<>();

  while (!q.isEmpty()) {
    int u = q.remove();
    visited.add(u);

    for (int v : graph.neighbors(u)) {
      if (graph.getColor(u, v) == WHITE) { 
        if (v == dest) return true; // White path exists
        q.add(v);
      }
    }
  }

  return false;
}

C++ example - Graph coloring:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
bool hasRainbowPath(Graph g, int src, int dest) {
  vector colors(7, false); // RGBYOP 
  return rainbowBFS(g, src, dest, colors);
}

bool rainbowBFS(Graph g, int u, int dest, vector<bool> colors) {
  queue<int> q; 
  q.push(u);
  colors[g.getColor(u)] = true; 

  while (!q.empty()) {
    int v = q.front(); q.pop();
    if (v == dest) return true;

    for (int next : g.adjList[v]) {
      // Enqueue neighbors of unused colors
    }
  }
  
  return false;
}

Python - Preferential attachment:

1
2
3
def hasRichFriendPath(graph, userA, userB):
  # Run BFS preferring high degree vertices
  return False # No rich friend path

The white-path theorem provides useful insights into structuring shortest paths in categorization problems.

The White-Path Theorem is a concept in graph theory and is particularly useful in the study of depth-first search (DFS) algorithms. According to this theorem, in a depth-first forest produced by a DFS, there is a white path between vertex (u) and vertex (v) if and only if (u) is an ancestor of (v). A white path is a path where all vertices, except possibly the first, are white, meaning they are undiscovered. This theorem helps in analyzing and understanding properties related to depth-first search in graphs.

The White-Path Theorem is related to Depth-First Search (DFS) in graph theory. The theorem states that in a DFS forest of a directed or undirected graph G, there is a path from vertex u to vertex v if and only if u is an ancestor of v in the DFS tree.

Visual Representation:

Imagine a directed graph as follows, where arrows show the direction from one vertex to another:

    A ----> B <---- C
    |               |
    v               v
    D <---- E ----> F

Perform a DFS starting from vertex A. The DFS tree may look something like this:

    A
    |
    B
    |
    D

In this DFS tree, according to the White-Path Theorem, there should be a white path (i.e., a path discovered during the DFS) from A to B and from A to D. This matches our original graph, confirming the theorem.

Key Takeaway:

The White-Path Theorem gives us a way to identify if there exists a path from one vertex to another by simply examining the DFS tree. This can be a powerful tool for understanding the structure of graphs and optimizing related algorithms.

Solution

Below are code examples that demonstrate the concept of the White-Path Theorem using depth-first search in Java, C++, and Python.

Java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.*;

public class WhitePathTheorem {
    private List<Integer>[] graph;
    private boolean[] visited;

    public WhitePathTheorem(int vertices) {
        graph = new LinkedList[vertices];
        visited = new boolean[vertices];

        for (int i = 0; i < vertices; ++i) {
            graph[i] = new LinkedList<>();
        }
    }

    public void addEdge(int u, int v) {
        graph[u].add(v);
    }

    public boolean dfs(int start, int target) {
        visited[start] = true;
        
        if (start == target) {
            return true;
        }

        for (int neighbor : graph[start]) {
            if (!visited[neighbor]) {
                if (dfs(neighbor, target)) {
                    return true;
                }
            }
        }
        return false;
    }
}

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <vector>
#include <list>

class WhitePathTheorem {
private:
    std::vector<std::list<int>> graph;
    std::vector<bool> visited;
public:
    WhitePathTheorem(int vertices) : graph(vertices), visited(vertices, false) {}

    void addEdge(int u, int v) {
        graph[u].push_back(v);
    }

    bool dfs(int start, int target) {
        visited[start] = true;

        if (start == target) {
            return true;
        }

        for (int neighbor : graph[start]) {
            if (!visited[neighbor]) {
                if (dfs(neighbor, target)) {
                    return true;
                }
            }
        }
        return false;
    }
};

Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class WhitePathTheorem:
    def __init__(self, vertices):
        self.graph = [[] for _ in range(vertices)]
        self.visited = [False] * vertices

    def add_edge(self, u, v):
        self.graph[u].append(v)

    def dfs(self, start, target):
        self.visited[start] = True

        if start == target:
            return True

        for neighbor in self.graph[start]:
            if not self.visited[neighbor]:
                if self.dfs(neighbor, target):
                    return True
        return False

Key Takeaways

  • The White-Path Theorem is directly related to the behavior of depth-first search in graphs.
  • It helps to identify whether there exists a white path between two vertices, aiding in graph analysis.
  • The code examples use depth-first search to illustrate the concept. A path between two vertices confirms the theorem.