TL;DR
Evaluating code similarity accurately is notoriously difficult because surface-level text and syntax trees often miss the actual execution logic. The authors introduce CSSG (Code Similarity using Semantic Graphs), a novel metric that builds enhanced Program Dependence Graphs (PDGs) to capture data and control flows. By calculating a constrained graph edit distance, CSSG significantly outperforms traditional lexical and syntax-based metrics in both monolingual and cross-lingual settings.
What Problem Does This Paper Address?
Whether you are building an automated grading system, detecting code clones, or evaluating large language models (LLMs) on code generation tasks, measuring how "similar" two pieces of code are is a fundamental challenge.
While functional correctness (running unit tests) is the gold standard, it requires a fully executable environment and curated test suites, making it useless for partial code fragments or intermediate generation steps. As a workaround, researchers often rely on text-matching metrics like BLEU or syntax-based metrics like Abstract Syntax Tree (AST) edit distances.
However, these approaches have a massive blind spot: they don't understand semantics. A simple variable rename or a shift in formatting can tank a lexical metric, while two programs with completely different execution logic can produce identical AST structures. This paper addresses the need for a similarity metric that evaluates what the code actually does rather than just how it looks.
Methods
The authors propose CSSG, which shifts the focus from syntax to semantics by leveraging Program Dependence Graphs (PDGs). The pipeline consists of four main steps:
- Semantic Graph Construction: Using the code analysis tool Joern, the method extracts function-level PDGs that explicitly map out data dependencies (how values move between variables) and control dependencies (how conditions govern statement execution).
- Graph Integration: To handle multi-function code snippets, a global root node is introduced, linking functions together via inter-procedural function call edges.
- Constrained Graph Matching: When comparing two code snippets, the algorithm enforces strict matching rules (e.g., function names must match exactly, and non-function nodes must share identical normalized semantic labels).
- Graph Edit Distance (GED) & Normalization: It calculates the minimum cost (insertions, deletions, substitutions) to transform one graph into the other. This distance is normalized against the maximum possible theoretical distance to yield a final bounded similarity score between 0 and 1.
Datasets
The authors evaluated their approach using CodeContests+, a large-scale dataset designed for competitive programming evaluation. It features:
- 11,690 competitive programming problems.
- Over 13 million submissions containing both correct and incorrect solutions.
From this dataset, the authors sampled thousands of test triplets (consisting of two correct solutions and one incorrect solution) across three languages: C++, Python, and Java.
Key Insights and Ideas
- ASTs Hide Execution Logic: The authors demonstrate that ASTs treat conditional statements and subsequent function calls as sequential peers, completely obscuring the causal relationship. CSSG fixes this by introducing explicit control dependency edges.
- Sensitivity to Data Flow: A minor change in variable usage (e.g., using
xinstead ofyin an assignment) will drastically alter the topology of a PDG, whereas an AST structure remains completely unchanged. - Cross-Lingual Robustness: Because data and control dependencies reflect the underlying algorithmic logic rather than language-specific syntax, graph-based representations are uniquely suited for cross-lingual evaluation (e.g., comparing a Python implementation to a C++ reference).
Key Results
To measure how effectively each metric distinguishes a correct solution from an incorrect one, the authors used Cohen’s $d$ effect size (where higher values indicate better discrimination):
- Monolingual Performance: CSSG consistently achieved the highest effect size across all tested languages (C++: 0.223, Python: 0.213, Java: 0.046), yielding an average effect size of 0.161, outperforming the AST-based TSED (0.123) and CodeBLEU (0.063).
- Cross-Lingual Performance: Cross-lingual tasks are notoriously brutal for similarity metrics due to massive syntactic divergence. CSSG was the only metric that maintained positive effect sizes across all language pairs, achieving the highest average cross-lingual effect size (0.108), compared to TSED (0.062) and CodeBLEU (-0.036).
Limitations and Future Work
While CSSG represents a major step forward, the authors note a few limitations:
- Dependency on Extraction Tools: The metric's accuracy is bound to the quality of the underlying graph extraction tool (Joern). Performance drops significantly in Java due to variations in how dependencies are extracted.
- Uniform Edit Costs: The current graph edit distance formulation treats all operations (node insertion vs. edge substitution) with equal cost. The authors acknowledge that optimizing non-uniform weights for different code patterns could yield even better results.
Summary updated