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.
'ais the type of the labels on nodes and'bis the type of the labels on edges. For an unlabeled graph, use(unit, unit) t.
- The type of a graph.
type nodeid = inttype edgeid = int * int
Methods
val empty : unit -> ('a, 'b) tval fromMaps : (nodeid, 'a) HashTable.hash_table * (edgeid, 'b) HashTable.hash_table -> ('a, 'b) tval fromMetisFile : string -> (unit, unit) tval addNode : ('a, 'b) t * int * 'a -> unitval updateNode : ('a -> 'a) * ('a, 'b) t * nodeid -> unitval addEdge : ('a, 'b) t * (int*int) * 'b -> unitval connected : ('a, 'b) t * (nodeid * nodeid) -> boolval getNodeById : ('a, 'b) t * nodeid -> 'a optionval getEdgeById : ('a, 'b) t * edgeid -> 'b optionval nodes : ('a, 'b) t -> 'a listval nodesi : ('a, 'b) t -> (nodeid * 'a) listval nodeids : ('a, 'b) t -> nodeid listval edges : ('a, 'b) t -> 'b listval edgesi : ('a, 'b) t -> (edgeid * 'b) listval edgeids : ('a, 'b) t -> edgeid listval neighbors : ('a, 'b) t * nodeid -> 'a listval neighborsi : ('a, 'b) t * nodeid -> (nodeid * 'a) listval graphFoldNodes : ('c * 'a -> 'c) * 'c * ('a, 'b) t -> 'cval graphFoldEdges : ('c * 'b -> 'c) * 'c * ('a, 'b) t -> 'cval graphFoldiNodes : ('c * (nodeid * 'a) -> 'c) * 'c * ('a, 'b) t -> 'cval graphFoldiEdges : ('c * (edgeid * 'b) -> 'c) * 'c * ('a, 'b) t -> 'cval neighborFoldNodes : ('c * 'a -> 'c) * 'c * (('a, 'b) t * nodeid) -> 'cval neighborFoldEdges : ('c * 'b -> 'c) * 'c * (('a, 'b) t * nodeid) -> 'cval neighborFoldiNodes : ('c * (nodeid * 'a) -> 'c) * 'c * (('a, 'b) t * nodeid) -> 'cval neighborFoldiEdges : ('c * (edgeid * 'b) -> 'c) * 'c * (('a, 'b) t * nodeid) -> 'cval graphMapNodes : ('a -> 'c) * ('a, 'b) t -> ('c, 'b) tval graphMapEdges : ('b -> 'd) * ('a, 'b) t -> ('a, 'd) tval graphMapiNodes : ((nodeid * 'a) -> 'c) * ('a, 'b) t -> ('c, 'b) tval graphMapiEdges : ((edgeid * 'b) -> 'd) * ('a, 'b) t -> ('a, 'd) tval graphModifyNodes : ('a -> 'a) * ('a, 'b) t -> unitval graphModifyEdges : ('b -> 'b) * ('a, 'b) t -> unitval graphModifyiNodes : (nodeid * 'a -> 'a) * ('a, 'b) t -> unitval graphModifyiEdges : (edgeid * 'b -> 'b) * ('a, 'b) t -> unitval numberOfNodes : ('a, 'b) t -> intval numberOfEdges : ('a, 'b) t -> intval copy: ('a, 'b) t -> ('a, 'b) tval 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
edgeMapmust 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
nodeIdwith the result of applyingfto 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 labelis returned. Otherwise,NONEis 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 labelis returned. Otherwise,NONEis 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
nodeIdin 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
nodeIdin 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.ftakes 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.ftakes 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, butfalso takes the node ID.
- Like
graphFoldiEdges (f, base, graph)- Like
graphFoldEdges, butfalso takes the edge ID.
- Like
neighborFoldNodes (f, base, (graph, nodeId))- Combines the labels on the nodes neighboring
nodeIdusingf, starting frombase.ftakes 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
nodeIdusingf, starting frombase.ftakes 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, butfalso takes the node ID.
- Like
neighborFoldiEdges (f, base, (graph, nodeId))- Like
neighborFoldEdges, butfalso 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 applyingfto 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 applyingfto the original edge labels.
- Creates a new graph with the same structure and node labels as
graphMapiNodes (f, graph)- Like
graphMapNodes, butfalso takes the node ID.
- Like
graphMapiEdges (f, graph)- Like
graphMapEdge, butfalso takes the edge ID.
- Like
graphModifyNodes (f, graph)- Updates the node labels of
graphwith the result of applyingfto the original node labels. This modifiesgraph.
- Updates the node labels of
graphModifyEdges (f, graph)- Updates the edge labels of
graphwith the result of applyingfto the original edge labels. This modifiesgraph.
- Updates the edge labels of
graphModifyiNodes (f, graph)- Like
graphModifyNodes, butfalso takes the node ID.
- Like
graphModifyiEdges (f, graph)- Like
graphModifyEdges, butfalso 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.