- 
                Notifications
    You must be signed in to change notification settings 
- Fork 114
          WIP: Add an is_chordal algorithm
          #434
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Draft
      
      
            Luis-Varona
  wants to merge
  3
  commits into
  JuliaGraphs:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
Luis-Varona:431-is_chordal-algorithm
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Draft
                    Changes from 1 commit
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| """ | ||
| is_chordal(g) | ||
|  | ||
| Check whether a graph is chordal. | ||
|  | ||
| A graph is said to be *chordal* if every cycle of length `≥ 4` has a chord | ||
| (i.e., an edge between two nodes not adjacent in the cycle). | ||
|  | ||
| ### Performance | ||
| This algorithm is linear in the number of vertices and edges of the graph (i.e., | ||
| it runs in `O(nv(g) + ne(g))` time). | ||
|  | ||
| ### Implementation Notes | ||
| `g` is chordal if and only if it admits a perfect elimination ordering—that is, | ||
| an ordering of the vertices of `g` such that for every vertex `v`, the set of | ||
| all neighbors of `v` that come later in the ordering forms a complete graph. | ||
| This is precisely the condition checked by the maximum cardinality search | ||
| algorithm [1], implemented herein. | ||
|  | ||
| We take heavy inspiration here from the existing Python implementation in [2]. | ||
|  | ||
| Not implemented for directed graphs, graphs with self-loops, or graphs with | ||
| parallel edges. | ||
|  | ||
| ### References | ||
| [1] Tarjan, Robert E. and Mihalis Yannakakis. "Simple Linear-Time Algorithms to | ||
| Test Chordality of Graphs, Test Acyclicity of Hypergraphs, and Selectively | ||
| Reduce Acyclic Hypergraphs." *SIAM Journal on Computing* 13, no. 3 (1984): | ||
| 566–79. https://doi.org/10.1137/0213035. | ||
| [2] NetworkX Developers. "is_chordal." NetworkX 3.5 documentation. NetworkX, | ||
| May 29, 2025. Accessed June 2, 2025. | ||
| https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.chordal.is_chordal.html. | ||
|  | ||
| # Examples | ||
| TODO: Add examples | ||
| """ | ||
| function is_chordal(g::AbstractSimpleGraph) | ||
| # The possibility of self-loops is already ruled out by the `AbstractSimpleGraph` type | ||
| is_directed(g) && throw(ArgumentError("Graph must be undirected")) | ||
| has_self_loops(g) && throw(ArgumentError("Graph must not have self-loops")) | ||
|  | ||
| # Every graph of order `< 4` has no cycles of length `≥ 4` and thus is trivially chordal | ||
| nv(g) < 4 && return true | ||
|  | ||
| unnumbered = Set(vertices(g)) | ||
| start_vertex = pop!(unnumbered) # The search can start from any arbitrary vertex | ||
| numbered = Set(start_vertex) | ||
|  | ||
| #= Searching by maximum cardinality ensures that in any possible perfect elimination | ||
| ordering of `g`, `purported_clique_nodes` is precisely the set of neighbors of `v` that | ||
| come later in the ordering. Hence, if the subgraph induced by `purported_clique_nodes` | ||
| in any iteration is not complete, `g` cannot be chordal. =# | ||
| while !isempty(unnumbered) | ||
| # `v` is the vertex in `unnumbered` with the most neighbors in `numbered` | ||
| v = _max_cardinality_node(g, unnumbered, numbered) | ||
| delete!(unnumbered, v) | ||
| push!(numbered, v) | ||
|  | ||
| # A complete subgraph of a larger graph is called a "clique," hence the naming here | ||
| purported_clique_nodes = intersect(neighbors(g, v), numbered) | ||
| purported_clique = induced_subgraph(g, purported_clique_nodes) | ||
|  | ||
| _is_complete_graph(purported_clique) || return false | ||
| end | ||
|  | ||
| #= That `g` admits a perfect elimination ordering is an "if and only if" condition for | ||
| chordality, so if every `purported_clique` was indeed complete, `g` must be chordal. =# | ||
| return true | ||
| end | ||
|  | ||
| function _max_cardinality_node( | ||
| g::AbstractSimpleGraph, unnumbered::Set{T}, numbered::Set{T} | ||
| ) where {T} | ||
| cardinality(v::T) = count(in(numbered), neighbors(g, v)) | ||
| return argmax(cardinality, unnumbered) | ||
| end | ||
|  | ||
| _is_complete_graph(g::AbstractSimpleGraph) = density(g) == 1 | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| @testset "Chordality" begin | ||
| # TODO: Add tests | ||
| end | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.