Comments (3)
Could you write a sentence or two about the outcome you observed and the solution your proposed in your PR?
from fsharp.fgl.
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.
fixed in 43a211d
from fsharp.fgl.
Related Issues (20)
- GDF files saved by gephi cannot be opend due to formating issues.
- Switch to new buildchain HOT 1
- Vertices module moved into the Graph module HOT 2
- [Docs] Error in example for documentation for Creating a Graph HOT 1
- [Feature Request] Consistent Namespace Naming
- [Feature Request] Modules eigenvector determination
- [Feature Request] Functions to calculate modularity on all available graph models
- [BUG] Directed graph: decompose only removes outgoing edges HOT 2
- [Feature Request] ArrayAdjacencyGraph Namespace names
- [BUG] Edges.fold processes edges twice in directed Graph HOT 5
- Error on creating directed graph HOT 2
- Centrality measures and shortest path HOT 5
- [Docs] `List.init` mistake in AdjacencyGraph tutorial
- [Feature Request] Call undirected edges in FGL.ArrayAdjacencyGraph HOT 1
- [Feature Request] Combine FGL and FGL.ArrayAdjacencyGraph into one Project
- [Feature Request]Add functions to convert FGL Graph into ArrayAdjacencyGraph
- [Feature Request]Add tests for undirected edge calls in FGL.ArrayAdjacencyGraph
- [Feature Request] Function to call complete Vertex List in ArrayAdjacencyGraph
- [Renaming required]
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fsharp.fgl.