Table of Contents
Fetching ...

CSSG: Measuring Code Similarity with Semantic Graphs

Jingwen Xu, Yiyang Lu, Changze Lv, Zisu Huang, Zhengkang Guo, Zhengyuan Wang, Muzhao Tian, Xuanjing Huang, Xiaoqing Zheng

TL;DR

CSSG addresses the limitation of surface- and syntax-focused code similarity by representing code as integrated program dependence graphs that capture data and control dependencies. It computes semantic similarity via a constrained graph edit distance and normalization, enabling cross-language semantic comparisons. On CodeContests+, CSSG achieves stronger discrimination between correct and incorrect solutions in both monolingual and cross-lingual settings, with stable positive effects across language pairs, highlighting the value of dependency-aware representations. This semantics-grounded approach offers a practical tool for evaluating code similarity and generation quality beyond surface lexical cues.

Abstract

Existing code similarity metrics, such as BLEU, CodeBLEU, and TSED, largely rely on surface-level string overlap or abstract syntax tree structures, and often fail to capture deeper semantic relationships between programs.We propose CSSG (Code Similarity using Semantic Graphs), a novel metric that leverages program dependence graphs to explicitly model control dependencies and variable interactions, providing a semantics-aware representation of code.Experiments on the CodeContests+ dataset show that CSSG consistently outperforms existing metrics in distinguishing more similar code from less similar code under both monolingual and cross-lingual settings, demonstrating that dependency-aware graph representations offer a more effective alternative to surface-level or syntax-based similarity measures.

CSSG: Measuring Code Similarity with Semantic Graphs

TL;DR

CSSG addresses the limitation of surface- and syntax-focused code similarity by representing code as integrated program dependence graphs that capture data and control dependencies. It computes semantic similarity via a constrained graph edit distance and normalization, enabling cross-language semantic comparisons. On CodeContests+, CSSG achieves stronger discrimination between correct and incorrect solutions in both monolingual and cross-lingual settings, with stable positive effects across language pairs, highlighting the value of dependency-aware representations. This semantics-grounded approach offers a practical tool for evaluating code similarity and generation quality beyond surface lexical cues.

Abstract

Existing code similarity metrics, such as BLEU, CodeBLEU, and TSED, largely rely on surface-level string overlap or abstract syntax tree structures, and often fail to capture deeper semantic relationships between programs.We propose CSSG (Code Similarity using Semantic Graphs), a novel metric that leverages program dependence graphs to explicitly model control dependencies and variable interactions, providing a semantics-aware representation of code.Experiments on the CodeContests+ dataset show that CSSG consistently outperforms existing metrics in distinguishing more similar code from less similar code under both monolingual and cross-lingual settings, demonstrating that dependency-aware graph representations offer a more effective alternative to surface-level or syntax-based similarity measures.
Paper Structure (17 sections, 5 equations, 5 figures, 3 tables)

This paper contains 17 sections, 5 equations, 5 figures, 3 tables.

Figures (5)

  • Figure 1: Computation pipeline for semantic graph edit distance. The process consists of four main steps: (1) utilizing Joern to extract function-level PDGs from the code snippet; (2) constructing a unified semantic graph by optimizing nodes and integrating a global root with function call edges; (3) computing the constrained Graph Edit Distancebetween the reference and target graphs; and (4) calculating the final similarity score by normalizing the GED against the maximum edit distance.
  • Figure 2: Pearson correlation heatmap among metrics on monolingual code pairs. The correlation patterns suggest that CSSG emphasizes semantic similarities that differ from surface-level metrics while remaining closely related to AST-based approaches.
  • Figure 3: The impact of data flow.
  • Figure 4: The impact of control flow
  • Figure 5: The impact of function call flow