Graphs
This library provides a undirected graph data structure and operations on that data structure. The API is similar to the GraphX library from Apache Spark.
Basic Usage
val g = BaseGraph.fromMetisFile("g.graph");
(* Compute the average degree of of graph by adding up the number of neighbors each node has... *)
val total = BaseGraph.graphFoldiNodes (( fn (sum, (nid, _)) => sum + (List.length (BaseGraph.neighbors (g, nid)))), 0, g)
(* ... and divide by the total number of nodes *)
val averageDegree = (Real.fromInt total) / (Real.fromInt (List.length (BaseGraph.nodes g)));
Interface
To use this library for undirected graphs, prefix the types and functions below
with BaseGraph.
. To use this library with directed, prefix the types and
functions below with DiGraph.
.
Types
type ('a, 'b) t
- The type of a graph.
'a
is the type of the labels on nodes and'b
is the type of the labels on edges. For an unlabeled graph, use(unit, unit) t
.
- The type of a graph.
type nodeid = int
type edgeid = int * int
Methods
val empty : unit -> ('a, 'b) t
val fromMaps : (nodeid, 'a) HashTable.hash_table * (edgeid, 'b) HashTable.hash_table -> ('a, 'b) t
val fromMetisFile : string -> (unit, unit) t
val addNode : ('a, 'b) t * int * 'a -> unit
val updateNode : ('a -> 'a) * ('a, 'b) t * nodeid -> unit
val addEdge : ('a, 'b) t * (int*int) * 'b -> unit
val connected : ('a, 'b) t * (nodeid * nodeid) -> bool
val getNodeById : ('a, 'b) t * nodeid -> 'a option
val getEdgeById : ('a, 'b) t * edgeid -> 'b option
val nodes : ('a, 'b) t -> 'a list
val nodesi : ('a, 'b) t -> (nodeid * 'a) list
val nodeids : ('a, 'b) t -> nodeid list
val edges : ('a, 'b) t -> 'b list
val edgesi : ('a, 'b) t -> (edgeid * 'b) list
val edgeids : ('a, 'b) t -> edgeid list
val neighbors : ('a, 'b) t * nodeid -> 'a list
val neighborsi : ('a, 'b) t * nodeid -> (nodeid * 'a) list
val graphFoldNodes : ('c * 'a -> 'c) * 'c * ('a, 'b) t -> 'c
val graphFoldEdges : ('c * 'b -> 'c) * 'c * ('a, 'b) t -> 'c
val graphFoldiNodes : ('c * (nodeid * 'a) -> 'c) * 'c * ('a, 'b) t -> 'c
val graphFoldiEdges : ('c * (edgeid * 'b) -> 'c) * 'c * ('a, 'b) t -> 'c
val neighborFoldNodes : ('c * 'a -> 'c) * 'c * (('a, 'b) t * nodeid) -> 'c
val neighborFoldEdges : ('c * 'b -> 'c) * 'c * (('a, 'b) t * nodeid) -> 'c
val neighborFoldiNodes : ('c * (nodeid * 'a) -> 'c) * 'c * (('a, 'b) t * nodeid) -> 'c
val neighborFoldiEdges : ('c * (edgeid * 'b) -> 'c) * 'c * (('a, 'b) t * nodeid) -> 'c
val graphMapNodes : ('a -> 'c) * ('a, 'b) t -> ('c, 'b) t
val graphMapEdges : ('b -> 'd) * ('a, 'b) t -> ('a, 'd) t
val graphMapiNodes : ((nodeid * 'a) -> 'c) * ('a, 'b) t -> ('c, 'b) t
val graphMapiEdges : ((edgeid * 'b) -> 'd) * ('a, 'b) t -> ('a, 'd) t
val graphModifyNodes : ('a -> 'a) * ('a, 'b) t -> unit
val graphModifyEdges : ('b -> 'b) * ('a, 'b) t -> unit
val graphModifyiNodes : (nodeid * 'a -> 'a) * ('a, 'b) t -> unit
val graphModifyiEdges : (edgeid * 'b -> 'b) * ('a, 'b) t -> unit
val numberOfNodes : ('a, 'b) t -> int
val numberOfEdges : ('a, 'b) t -> int
val copy: ('a, 'b) t -> ('a, 'b) t
val toString : ('a, 'b) t -> string
Method Overview
empty ()
- Creates a new graph with no nodes and no edges.
fromMaps (nodeMap, edgeMap)
- Creates a new graph with the given node and edge maps. All nodes referenced
in
edgeMap
must appear innodeMap
. The edge map must use hash and equality functions that are compatible with the kind of graph being created (i.e., order-agnostic for undirected graphs and order-checking for directed graphs).
- Creates a new graph with the given node and edge maps. All nodes referenced
in
fromMetisFile fileName
- Creats a graph from a METIS file.
addNode (graph, nodeId, nodeLabel)
- Adds a node to
graph
. If a node with the same identifier already existed in the graph, the result is undefined.
- Adds a node to
updateNode (f, graph, nodeId)
- Updates the label of the node identified by
nodeId
with the result of applyingf
to the node’s original label. This modifiesgraph
.
- Updates the label of the node identified by
addEdge (graph, edgeId, edgeLabel)
- Adds an edge to a graph.
connected (graph, (nodeIdA, nodeIdB))
- Returns true if the given nodes are adjacent.
getNodeById (graph, nodeId)
- Gets the label of the node. If the node exists,
SOME label
is returned. Otherwise,NONE
is returned.
- Gets the label of the node. If the node exists,
getEdgeById (graph, edgeId)
- Gets the label of the edge. If the edge exists,
SOME label
is returned. Otherwise,NONE
is returned.
- Gets the label of the edge. If the edge exists,
nodes graph
- Returns a list of all nodes labels in the graph. If more than one node has a given label, the label will appear multiple times in the list.
nodesi graph
- Returns a list of all node ids in the graph with their associated labels.
nodeids graph
- Returns a list of all node ids in the graph.
edges graph
- Returns a list of all edge labels in the graph. If more than one edge has a given label, the label will appear multiple times in the list.
edgesi graph
- Returns a list of all edge ids in the graph with their associated labels.
edgeids graph
- Returns a list of all edge ids in the graph.
neighbors (graph, nodeId)
- Returns a list of the labels of the nodes adjacent to
nodeId
in the graph.
- Returns a list of the labels of the nodes adjacent to
neighborsi graph
- Returns a list of the node ids of the nodes adjacent to
nodeId
in the graph with their associated labels.
- Returns a list of the node ids of the nodes adjacent to
graphFoldNodes (f, base, graph)
- Combines the labels on the nodes using
f
, starting frombase
.f
takes a pair of the result so far and the next label as arguments and returns the result including that label.
- Combines the labels on the nodes using
graphFoldEdges (f, base, graph)
- Combines the labels on the edges using
f
, starting frombase
.f
takes a pair of the result so far and the next label as arguments and returns the result including that label.
- Combines the labels on the edges using
graphFoldiNodes (f, base, graph)
- Like
graphFoldNodes
, butf
also takes the node ID.
- Like
graphFoldiEdges (f, base, graph)
- Like
graphFoldEdges
, butf
also takes the edge ID.
- Like
neighborFoldNodes (f, base, (graph, nodeId))
- Combines the labels on the nodes neighboring
nodeId
usingf
, starting frombase
.f
takes a pair of the result so far and the next label as arguments and returns the result including that label.
- Combines the labels on the nodes neighboring
neighborFoldEdges (f, base, (graph, nodeId))
- Combines the labels on the edges incident to
nodeId
usingf
, starting frombase
.f
takes a pair of the result so far and the next label as arguments and returns the result including that label.
- Combines the labels on the edges incident to
neighborFoldiNodes (f, base, (graph, nodeId))
- Like
neighborFoldNodes
, butf
also takes the node ID.
- Like
neighborFoldiEdges (f, base, (graph, nodeId))
- Like
neighborFoldEdges
, butf
also takes the edge ID.
- Like
graphMapNodes (f, graph)
- Creates a new graph with the same structure and edge labels as
graph
, but whose node labels are the result of applyingf
to the original node labels.
- Creates a new graph with the same structure and edge labels as
graphMapEdges (f, graph)
- Creates a new graph with the same structure and node labels as
graph
, but whose edge labels are the result of applyingf
to the original edge labels.
- Creates a new graph with the same structure and node labels as
graphMapiNodes (f, graph)
- Like
graphMapNodes
, butf
also takes the node ID.
- Like
graphMapiEdges (f, graph)
- Like
graphMapEdge
, butf
also takes the edge ID.
- Like
graphModifyNodes (f, graph)
- Updates the node labels of
graph
with the result of applyingf
to the original node labels. This modifiesgraph
.
- Updates the node labels of
graphModifyEdges (f, graph)
- Updates the edge labels of
graph
with the result of applyingf
to the original edge labels. This modifiesgraph
.
- Updates the edge labels of
graphModifyiNodes (f, graph)
- Like
graphModifyNodes
, butf
also takes the node ID.
- Like
graphModifyiEdges (f, graph)
- Like
graphModifyEdges
, butf
also takes the edge ID.
- Like
numberOfNodes graph
- Returns the number of nodes in the graph.
numberOfEdges graph
- Returns the number of edges in the graph.
copy graph
- Creates a copy of a graph. Modifications to the original graph will not affect the copy and modifications to the copy will not affect the original graph.
toString graph
- Converts a graph to a string.