UltipaDocs
Try Playground
  • Introduction
  • Managing HDC Graphs
  • Managing Distributed Projections
  • Installing Algorithms
  • Running Algorithms
    • Degree Centrality
    • Closeness Centrality
    • Harmonic Centrality
    • Graph Centrality
    • Betweenness Centrality
    • Eigenvector Centrality
    • Katz Centrality
    • CELF
    • PageRank
    • ArticleRank
    • TextRank
    • HITS
    • SybilRank
    • Jaccard Similarity
    • Overlap Similarity
    • Cosine Similarity
    • Pearson Correlation Coefficient
    • Euclidean Distance
    • K-Hop All
    • Bipartite Graph
    • HyperANF
    • Connected Component
    • Triangle Counting
    • Induced Subgraph
    • k-Core
    • k-Truss
    • p-Cohesion
    • k-Edge Connected Components
    • Local Clustering Coefficient
    • Topological Sort
    • Schema Overview
    • Dijkstra's Single-Source Shortest Path
    • Delta-Stepping Single-Source Shortest Path
    • Shortest Path Faster Algorithm (SPFA)
    • Minimum Spanning Tree
    • Breadth-First Search (BFS)
    • Depth-First Search (DFS)
    • Adamic-Adar Index
    • Common Neighbors
    • Preferential Attachment
    • Resource Allocation
    • Total Neighbors
    • Louvain
    • Leiden
    • Label Propagation
    • HANP
    • k-Means
    • kNN (k-Nearest Neighbors)
    • K-1 Coloring
    • Conductance
      • Random Walk
      • Node2Vec Walk
      • Node2Vec
      • Struc2Vec Walk
      • Struc2Vec
      • LINE
      • Fast Random Projection
      • Summary of Graph Embedding
      • Gradient Descent
      • Backpropagation
      • Skip-gram
      • Skip-gram Optimization
  1. Docs
  2. /
  3. Graph Analytics & Algorithms
  4. /
  5. Community Detection & Classification

K-1 Coloring

HDC

Overview

The K-1 Coloring algorithm assigns colors to nodes so that no two adjacent nodes share the same color, while minimizing the total number of colors used.

  • U.V. Çatalyürek, J. Feo, A.H. Gebremedhin, M. Halappanavar, A. Pothen, Graph Coloring Algorithms for Multi-core and Massively Multithreaded Architectures (2018)

Concepts

Distance-1 Graph Coloring

Distance-1 graph coloring, also known as K-1 graph coloring, is a concept in graph theory where the goal is to assign colors (represented by integers 0, 1, 2, ...) to the nodes of a graph such that no two nodes at distance 1 from each other (i.e., adjacent nodes) share the same color. The objective is also to minimize the number of colors used.

One of the most famous applications of graph coloring is geographical map coloring, where regions on a map are represented as nodes, and edges connect adjacent regions (those sharing a border). The task is to color the regions so that no two adjacent regions have the same color.

This concept has many practical applications beyond maps. For example, in school scheduling, each class is represented as a node, and edges indicate conflicts (such as two classes needing the same room). By coloring the graph, each class is assigned a "color" that represents a different time slot, ensuring no two conflicting classes are scheduled simultaneously.

Greedy Coloring Algorithm

The graph coloring problem is NP-hard to solved optimally, but near-optimal solutions that are near-optimal can be obtained using a greedy algorithm.

Serial Greedy Coloring Algorithm

At the beginning of the greedy algorithm, each node v in the graph is initialized as uncolored. The algorithm processes each node v as below:

  • For every adjacent node w of v, mark the color of w as forbidden for v.
  • Assign the smallest available color to v that is different from all its forbidden colors.

The algorithm assigns colors to nodes sequentially, which may become a bottleneck for large graphs. To address this, the following algorithm allows multiple nodes to be processed in parallel, with mechanisms to handle potential conflicts.

Iterative Parallel Greedy Coloring Algorithm

The iterative parallel greedy coloring algorithm is a parallel extension of the traditional serial greedy coloring algorithm. It is designed to leverage modern multicore and distributed computing systems, enabling more efficient processing of large graphs.

The algorithm divides the nodes in the graph into independent sets to allow concurrent processing across multiple threads. Each iteration has two phases:

  1. Tentative coloring phase: Similar to the serial greedy coloring algorithm, this phase assigns colors to nodes, but does so in parallel across multiple threads.
  2. Conflict detection phase: Each thread checks for coloring conflicts, i.e., cases where adjacent nodes (processed in different threads) have been assigned the same color. Conflicted nodes are marked for re-coloring in the next iteration.

The algorithm repeats these phases until no more nodes need to be re-colored.

Considerations

  • The K-1 Coloring algorithm treats all edges as undirected, ignoring their original direction.

Example Graph

Run the following statements on an empty graph to define its structure and insert data:

INSERT (A:default {_id: "A"}),
       (B:default {_id: "B"}),
       (C:default {_id: "C"}),
       (D:default {_id: "D"}),
       (E:default {_id: "E"}),
       (F:default {_id: "F"}),
       (G:default {_id: "G"}),
       (H:default {_id: "H"}),
       (A)-[:default]->(B),
       (A)-[:default]->(C),
       (A)-[:default]->(D),
       (A)-[:default]->(E),
       (A)-[:default]->(G),
       (D)-[:default]->(E),
       (D)-[:default]->(F),
       (E)-[:default]->(F),
       (G)-[:default]->(D),
       (G)-[:default]->(H);

Creating HDC Graph

To load the entire graph to the HDC server hdc-server-1 as my_hdc_graph:

CREATE HDC GRAPH my_hdc_graph ON "hdc-server-1" OPTIONS {
  nodes: {"*": ["*"]},
  edges: {"*": ["*"]},
  direction: "undirected",
  load_id: true,
  update: "static"
}

Parameters

Algorithm name: k1_coloring

Name
Type
Spec
Default
Optional
Description
loop_numInteger≥15YesNumber of iterations. The algorithm will terminate after completing all rounds. This option is only valid when version is set to 2.
versionInteger1, 22YesSet to 1 to run the serial greedy coloring algorithm, or 2 to run the iterative parallel greedy coloring algorithm.
return_id_uuidStringuuid, id, bothuuidYesIncludes _uuid, _id, or both to represent nodes in the results.

In the results of this algorithm, nodes with the same color are considered to belong to the same community.

File Writeback

This algorithm can generate three files:

Spec
Content
filename_community_id
  • _id/_uuid: The node.
  • community_id: ID of the community to which the node belongs.
filename_ids
  • community_id: ID of each community.
  • _ids/_uuids: Nodes in each community.
filename_num
  • community_id: ID of each commnity.
  • count: Number of nodes in each community.
CALL algo.k1_coloring.write("my_hdc_graph", {
  return_id_uuid: "id",
  version: 1
}, {
  file: {
    filename_community_id: "f1.txt",
    filename_ids: "f2.txt",
    filename_num: "f3.txt"
  }
})

Result:

_id,community_id
D,1
F,2
H,1
B,1
A,2
E,0
C,0
G,0

DB Writeback

Writes the community_id values from the results to the specified node property. The property type is uint32.

CALL algo.k1_coloring.write("my_hdc_graph", {
  loop_num: 10,
  version: 2
}, {
  db: {
    property: "color"
  }
})

Stats Writeback

CALL algo.k1_coloring.write("my_hdc_graph", {
  version: 1
}, {
  stats: {}
})

Result:

community_count
3

Full Return

CALL algo.k1_coloring.run("my_hdc_graph", {
  return_id_uuid: "id",
  loop_num: 5,
  version: 2
}) YIELD r
RETURN r

Result:

_idcommunity_id
D1
F2
H1
B1
A2
E0
C0
G0

Stream Return

exec{
  algo(k1_coloring).params({
    return_id_uuid: "id",
    loop_num: 15,
    version: 1
  }).stream() as r
  group by r.community_id as communityID
  with r, count(r) as nodeCounts
  return table(communityID, nodeCounts)
} on my_hdc_graph

Result:

communityIDnodeCounts
03
13
22

Stats Return

CALL algo.k1_coloring.stats("my_hdc_graph") YIELD res
RETURN res

Result:

community_count
3