Giter VIP home page Giter VIP logo

Comments (3)

HLWeil avatar HLWeil commented on July 23, 2024

Could you write a sentence or two about the outcome you observed and the solution your proposed in your PR?

from fsharp.fgl.

LibraChris avatar LibraChris commented on July 23, 2024

Observed Outcome

For the example above the observed outcome is:

val it : int [] [] =
  [|[|1; 1; 0; 1; 1; 0; 0; 0; 0|]; [|0; 0; 1; 0; 1; 0; 0; 0; 0|];
    [|0; 0; 0; 1; 0; 1; 0; 0; 0|]; [|0; 0; 0; 0; 1; 0; 0; 0; 0|];
    [|0; 0; 0; 0; 0; 0; 0; 0; 0|]; [|0; 0; 0; 0; 0; 0; 1; 1; 1|];
    [|0; 0; 0; 0; 0; 0; 0; 1; 0|]; [|0; 0; 0; 0; 0; 0; 0; 0; 1|];
    [|0; 0; 0; 0; 0; 0; 0; 0; 0|]|]

Expected Outcome

For the example above the expected outcome would be:

val it : int [] [] =
  [|[|1; 1; 0; 1; 1; 0; 0; 0; 0|]; [|1; 0; 1; 0; 1; 0; 0; 0; 0|];
    [|0; 1; 0; 1; 0; 1; 0; 0; 0|]; [|1; 0; 1; 0; 1; 0; 0; 0; 0|];
    [|1; 1; 0; 1; 0; 0; 0; 0; 0|]; [|0; 0; 1; 0; 0; 0; 1; 1; 1|];
    [|0; 0; 0; 0; 0; 1; 0; 1; 0|]; [|0; 0; 0; 0; 0; 1; 1; 0; 1|];
    [|0; 0; 0; 0; 0; 1; 0; 1; 0|]|]

Solution

I propose minor additions to the very last functions of the code shown below :

from

    ///Transforms a graph into an adjacency matrix of its edges.
    let inline toAdjacencyMatrix (g:Graph<'Vertex,'Label,'Edge>) =
        //Create a hashmap of the vertices
        let hashMap = System.Collections.Generic.Dictionary<'Vertex,int>()
        let n = 
            let rec loop g i= 
                match Graph.decomposeFirst g with
                | Some (_,v,_,_),g -> 
                    hashMap.[v] <- i
                    loop g (i+1)        
                | None, _ -> i
            loop g 0
        //Create the matrix
        let adj : 'Edge [][] = Array.init n (fun _ -> 
            Array.zeroCreate n)
        //Fill the matrix with values by using the hashmap as an index finder
        Edges.iter (fun v1 v2 e -> adj.[hashMap.Item v1].[hashMap.Item v2] <- e) g
        adj

    ///Transfroms a graph into a adjacency matrix, maps every edge using the projection.
    let inline toAdjacencyMatrixBy (projection : 'Edge -> 'REdge) (g:Graph<'Vertex,'Label,'Edge>) =
        //Create a hashmap of the vertices
        let hashMap = System.Collections.Generic.Dictionary<'Vertex,int>()
        let n = 
            let rec loop g i= 
                match Graph.decomposeFirst g with
                | Some (_,v,_,_),g -> 
                    hashMap.[v] <- i
                    loop g (i+1)        
                | None, _ -> i+1
            loop g 0
        //Create the matrix
        let adj : 'REdge [][] = Array.init n (fun _ -> 
            Array.zeroCreate n)
        //Fill the matrix with values by using the hashmap as an index finder
        Edges.iter (fun v1 v2 e -> adj.[hashMap.Item v1].[hashMap.Item v2] <- projection e) g
        adj

To

    ///Transforms a graph into a adjacency matrix of its edges. If there is no edge between two vertices, the noEdgeValue is used.
    let inline toAdjacencyMatrix (g:Graph<'Vertex,'Label,'Edge>) =
        //Create a hashmap of the vertices
        let hashMap = System.Collections.Generic.Dictionary<'Vertex,int>()
        let n = 
            let rec loop g i= 
                match Graph.decomposeFirst g with
                | Some (_,v,_,_),g -> 
                    hashMap.[v] <- i
                    loop g (i+1)        
                | None, _ -> i
            loop g 0
        //Create the matrix
        let adj : 'Edge [][] = Array.init n (fun _ -> 
            Array.zeroCreate n)
        //Fill the matrix with values by using the hashmap as an index finder
        Edges.iter (fun v1 v2 e -> 
            adj.[hashMap.Item v1].[hashMap.Item v2] <- e
            adj.[hashMap.Item v2].[hashMap.Item v1] <- e
            ) g
        adj

    ///Transfroms a graph into a adjacency matrix, maps every edge using the projection.
    let inline toAdjacencyMatrixBy (projection : 'Edge -> 'REdge) (g:Graph<'Vertex,'Label,'Edge>) =
        //Create a hashmap of the vertices
        let hashMap = System.Collections.Generic.Dictionary<'Vertex,int>()
        let n = 
            let rec loop g i= 
                match Graph.decomposeFirst g with
                | Some (_,v,_,_),g -> 
                    hashMap.[v] <- i
                    loop g (i+1)        
                | None, _ -> i+1
            loop g 0
        //Create the matrix
        let adj : 'REdge [][] = Array.init n (fun _ -> 
            Array.zeroCreate n)
        //Fill the matrix with values by using the hashmap as an index finder
        Edges.iter (fun v1 v2 e -> 
            let e' = projection e
            adj.[hashMap.Item v1].[hashMap.Item v2] <- e'
            adj.[hashMap.Item v2].[hashMap.Item v1] <- e'
            ) g
        adj

from fsharp.fgl.

HLWeil avatar HLWeil commented on July 23, 2024

fixed in 43a211d

from fsharp.fgl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.