Abstract
In this paper, we explore the feasibility of finding algorithm implementations from code. Successfully matching code and algorithms can help understand unknown code, provide reference implementations, and automatically collect data for learning-based program synthesis. To achieve the goal, we designed a new language named p-language to specify the algorithms and a static analyzer for the p-language to automatically extract control flow, math, and natural language information from the algorithm descriptions. We embedded the output of p-language (p-code) and source code in a common vector space using self-supervised machine learning methods to match algorithm with code without any manual annotation. We developed a tool named Beryllium. It takes pseudo code as a query and returns a list of ranked code snippets that likely match the algorithm query. Our evaluation on Stony Brook Algorithm Repository and popular GitHub projects show that Beryllium significantly outperformed the state-of-the-art code search tools in both C and Java. Specifically, for 98.5%, 93.8%, and 66.2% queries, we found the algorithm implementations in the top 25, 10, and 1 ranked list, respectively. Given 87 algorithm queries, we found implementations for 74 algorithms in the GitHub projects where we did not know the algorithms before.
Introduction
Software reuse is a cornerstone of modern engineering, but finding the "right" implementation of a specific algorithm in a massive codebase like GitHub is surprisingly difficult. Current search tools generally fall into two traps: keyword dependency and coarse granularity. Keyword-based search relies on developers naming their functions accurately (e.g., searching for "Kruskal" only works if the author named it that), while many neural search tools return entire methods or files, which is unhelpful if the algorithm is just a small part of a complex function.
The authors identify a "semantic gap" between high-level algorithmic logic and low-level implementation details. Beryllium aims to bridge this gap by using pseudo-code—which is structured, language-independent, and focused on logic—as the search query.
Methodology
Beryllium’s architecture is a multi-modal pipeline that translates both pseudo-code and source code into a shared vector space.
The p-language and ICFGs
The authors designed a new intermediate language, the p-language, to represent algorithms. It captures control flow (loops, branches), mathematical expressions, and natural language descriptions. The tool converts both the pseudo-code query and the target source code into Interprocedural Control Flow Graphs (ICFGs). This allows the system to analyze the logical structure of the code rather than just the text.
Representation Learning
To map these structures into a vector space, Beryllium uses three specialized encoders:
- Control Flow: A Graph Autoencoder (GAE) learns the structural "shape" of the code. It is trained via self-supervised learning (predicting links between nodes), removing the need for manual labeling.
- Natural Language: CodeBERT aligns the descriptive text in pseudo-code with the semantics of the source code.
- Math Encoding: A custom encoder maps mathematical operators into seven semantic categories to group related operations (e.g.,
+and-).
Fragment Retrieval
Instead of just finding similar lines, Beryllium uses agglomerative hierarchical clustering. It identifies clusters of matched nodes that are physically close within the ICFG, allowing it to extract specific code fragments that implement the query logic.
Evaluation and Results
The authors evaluated Beryllium using 103 algorithm queries sourced from classic textbooks like Introduction to Algorithms (CLRS). The tool was tested against the Stony Brook Algorithm Repository (ground-truth C and Java projects) and a GitHub database of 20 large-scale projects.
Beryllium significantly outperformed state-of-the-art baselines like Deep Code Search (DCS) and OCoR:
- High Precision: For the 65 algorithm queries tested against the Stony Brook database, Beryllium found the correct implementation in the Top 1 result 66.2% of the time and in the Top 25 results 98.5% of the time.
- Real-World Discovery: When searching the GitHub database, Beryllium successfully located implementations for 74 out of 87 algorithms, many of which were previously undocumented in those projects.
- Language Agnostic: The tool proved effective for both C and Java, even in "mixed" settings where the model was trained on one language and queried against another.
Conclusion
Beryllium demonstrates that pseudo-code is an ideal "Goldilocks" query—more precise than natural language but more flexible than code snippets. By focusing on structure over syntax and leveraging self-supervised learning, the system can be adapted to new languages without massive, manually labeled datasets. Future work includes using the discovered algorithm-to-code pairs to create better datasets for automated program synthesis.
Summary updated