Yog.FSharp
Getting Started Examples API Reference GitHub

Gdf Module

GDF (GUESS Graph Format) serialization support.

Provides functions to serialize graphs in GDF format, a simple text-based format used by Gephi and other graph visualization tools. GDF uses a column-based format similar to CSV with separate sections for nodes and edges.

Format Overview

GDF files consist of two sections: - nodedef> - Defines node columns and data - edgedef> - Defines edge columns and data

Example

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
open Yog.IO
open Yog.Model

// Create a simple graph
let graph =
    empty Directed
    |> addNode 1 "Alice"
    |> addNode 2 "Bob"
    |> addEdge 1 2 5

// Serialize to GDF
let gdf = Gdf.serialize graph
File.WriteAllText("graph.gdf", gdf)

Output Format

1: 
2: 
3: 
4: 
5: 
nodedef>name VARCHAR,label VARCHAR
1,Alice
2,Bob
edgedef>node1 VARCHAR,node2 VARCHAR,weight VARCHAR
1,2,5
namespace Yog
namespace Yog.IO
module Model from Yog
<summary> Core graph data structures and basic operations for the yog library. This module defines the fundamental `Graph` type and provides all basic operations for creating and manipulating graphs. The graph uses an adjacency list representation with dual indexing (both outgoing and incoming edges) for efficient traversal in both directions. </summary>
val graph: Graph<string,int>
val empty: graphType: GraphType -> Graph<'n,'e>
<summary> Creates a new empty graph of the specified type. ## Example ```fsharp let graph = Model.empty Directed ``` </summary>
union case GraphType.Directed: GraphType
<summary> A directed graph where edges have a direction from source to destination. </summary>
val addNode: id: NodeId -> data: 'n -> graph: Graph<'n,'e> -> Graph<'n,'e>
<summary> Adds a node to the graph with the given ID and data. If a node with this ID already exists, its data will be replaced. ## Example ```fsharp graph |&gt; addNode 1 "Node A" |&gt; addNode 2 "Node B" ``` </summary>
val addEdge: src: NodeId -> dst: NodeId -> weight: 'e -> graph: Graph<'n,'e> -> Graph<'n,'e>
<summary> Adds an edge to the graph with the given weight. For directed graphs, adds a single edge from src to dst. For undirected graphs, adds edges in both directions. &gt; **Note:** If `src` or `dst` have not been added via `addNode`, &gt; the edge will still be created in the edge dictionaries, but the &gt; nodes will be missing from `graph.Nodes`. This creates "ghost nodes" &gt; that are traversable but invisible to functions that iterate over &gt; nodes (e.g. `order`, `allNodes`). Use `addEdgeEnsured` to &gt; auto-create missing endpoints with a default value. ## Example ```fsharp graph |&gt; addEdge 1 2 10 ``` </summary>
val gdf: string
module Gdf from Yog.IO
<summary> GDF (GUESS Graph Format) serialization support. Provides functions to serialize graphs in GDF format, a simple text-based format used by Gephi and other graph visualization tools. GDF uses a column-based format similar to CSV with separate sections for nodes and edges. ## Format Overview GDF files consist of two sections: - **nodedef&gt;** - Defines node columns and data - **edgedef&gt;** - Defines edge columns and data ## Example ```fsharp open Yog.IO open Yog.Model // Create a simple graph let graph = empty Directed |&gt; addNode 1 "Alice" |&gt; addNode 2 "Bob" |&gt; addEdge 1 2 5 // Serialize to GDF let gdf = Gdf.serialize graph File.WriteAllText("graph.gdf", gdf) ``` ## Output Format ``` nodedef&gt;name VARCHAR,label VARCHAR 1,Alice 2,Bob edgedef&gt;node1 VARCHAR,node2 VARCHAR,weight VARCHAR 1,2,5 ``` </summary>
val serialize: graph: Graph<string,string> -> string
<summary> Serializes a graph to GDF format where node and edge data are strings. This is a simplified version of `serializeWith` for graphs where node data and edge data are already strings. The string data is used as the "label" attribute for both nodes and edges. **Time Complexity:** O(V + E) ## Example ```fsharp let graph = empty Directed |&gt; addNode 1 "Alice" |&gt; addNode 2 "Bob" |&gt; addEdge 1 2 "friend" let gdf = Gdf.serialize graph File.WriteAllText("graph.gdf", gdf) ``` </summary>

Types

Type Description

Options

Options for GDF serialization.

Functions and values

Function or value Description

defaultOptions

Full Usage: defaultOptions

Returns: Options

Default GDF serialization options.

Returns: Options

serialize graph

Full Usage: serialize graph

Parameters:
    graph : Graph<string, string>

Returns: string

Serializes a graph to GDF format where node and edge data are strings.

This is a simplified version of serializeWith for graphs where node data and edge data are already strings. The string data is used as the "label" attribute for both nodes and edges.

Time Complexity: O(V + E)

Example

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
let graph =
    empty Directed
    |> addNode 1 "Alice"
    |> addNode 2 "Bob"
    |> addEdge 1 2 "friend"

let gdf = Gdf.serialize graph
File.WriteAllText("graph.gdf", gdf)
val graph: obj
val gdf: obj

graph : Graph<string, string>
Returns: string

serializeWeighted graph

Full Usage: serializeWeighted graph

Parameters:
    graph : Graph<string, int>

Returns: string

Serializes a graph to GDF format with integer edge weights.

This is a convenience function for the common case of graphs with integer weights. Node data is used as labels, and edge weights are serialized to the "weight" column.

Time Complexity: O(V + E)

Example

1: 
2: 
3: 
4: 
5: 
6: 
7: 
let graph =
    empty Directed
    |> addNode 1 "Alice"
    |> addNode 2 "Bob"
    |> addEdge 1 2 5

let gdf = Gdf.serializeWeighted graph
val graph: obj
val gdf: obj

graph : Graph<string, int>
Returns: string

serializeWith nodeAttr edgeAttr options graph

Full Usage: serializeWith nodeAttr edgeAttr options graph

Parameters:
    nodeAttr : 'n -> (string * string) list
    edgeAttr : 'e -> (string * string) list
    options : Options
    graph : Graph<'n, 'e>

Returns: string

Serializes a graph to GDF format with custom attribute mappers and options.

This function allows you to control how node and edge data are converted to GDF attributes, and customize the output format.

Time Complexity: O(V + E)

Example

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
type Person = { Name: string; Age: int }
type Connection = { Weight: int; Type: string }

let graph =
    empty Directed
    |> addNode 1 { Name = "Alice"; Age = 30 }
    |> addNode 2 { Name = "Bob"; Age = 25 }
    |> addEdge 1 2 { Weight = 5; Type = "friend" }

let nodeAttrs p = ["label", p.Name; "age", string p.Age]
let edgeAttrs c = ["weight", string c.Weight; "type", c.Type]

let gdf = Gdf.serializeWith nodeAttrs edgeAttrs defaultOptions graph

Use Cases

  • Exporting graphs for Gephi visualization
  • Simple text-based graph interchange format
  • Easy to parse and generate programmatically
type Person = { Name: string Age: int }
Multiple items
val string: value: 'T -> string

--------------------
type string = System.String
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
type Connection = { Weight: int Type: string }
val graph: obj
val nodeAttrs: p: Person -> (string * string) list
val p: Person
Person.Name: string
Person.Age: int
val edgeAttrs: c: Connection -> (string * string) list
val c: Connection
Connection.Weight: int
Connection.Type: string
val gdf: obj

nodeAttr : 'n -> (string * string) list
edgeAttr : 'e -> (string * string) list
options : Options
graph : Graph<'n, 'e>
Returns: string

serializeWithOptions options graph

Full Usage: serializeWithOptions options graph

Parameters:
Returns: string

Serializes a graph to GDF format with custom options.

Uses default attribute mapping (single "label" column) but allows customization of separator and type annotations.

Time Complexity: O(V + E)

Example

1: 
2: 
let options = { defaultOptions with Separator = "\t"; IncludeTypes = false }
let gdf = Gdf.serializeWithOptions options graph
val options: 'a
val gdf: obj

options : Options
graph : Graph<string, string>
Returns: string