Vertices and edges have the same behavior as structures. Dot notation object.value can be used to access their properties.
Return a newly created graph.
graph() : graph
Load a graph from a file (file format).
loadFromFile(graph, string) : bool
Check if a graph is directed.
isDirected(graph) : bool|null
Set a new value of directed flag and return the previous value.
setDirected(graph, number) : bool|null
Invert the direction of edges in a graph.
invertEdgesDirection(graph) : null
Create a new vertex in a graph.
generateVertex(graph) : vertex|null
Create a new edge in a graph.
generateEdge(graph, vertex, vertex) : edge|null
Delete a vertex from a graph.
deleteVertex(graph, vertex) : null
Delete an edge from a graph.
deleteEdge(graph, edge) : null
Get count of vertices in a graph.
getNumVertices(graph) : int|null
Get count of edges in a graph.
getNumEdges(graph) : int|null
Get graph's vertices as a set object.
getVertices(graph) : set|null
Get graph's edges as a set object.
getEdges(graph) : set|null
Get degree of a vertex.
getDegree(vertex) : int|null
Get all neighbors of a vertex as a set object.
getNeighbors(vertex) : set|null
Get the begin vertex of an edge. If the graph is not directed, one of the edge's vertices will be returned.
getBeginVertex(edge) : vertex|null
Get the end vertex of an edge. If the graph is not directed, one of the edge's vertices will be returned.
getEndVertex(edge) : vertex|null
Create adjacency matrix from a graph. 2D array (array containing arrays in all items) will be returned.
getAdjacencyMatrix(graph) : array
Set property with name string and value object to all vertices of a graph.
setPropertyToAllVertices(graph, string, object) : null
Set property with name string and value object to all edges of a graph.
setPropertyToAllEdges(graph, string, object) : null
Manual graph generation and recursive depth first search.
define("NUM_VERTICES", "10"); // In one layer define("NEW", "0"); define("CLOSED", "1"); // Recursive depth first search function dfs(v) { if(v.state == CLOSED) return; echo("Closing vertex: " + v.num + "\n"); v.state = CLOSED; foreach(neighbor; v.getNeighbors()) dfs(neighbor); } // Generate testing graph and run DFS function main(argv) { // o ----- o ----- o ----- ... // | | | // | | | // o o o g = graph(); v1 = g.generateVertex(); v1.num = 0; first = v1; // Generate first layer for(i = 1; i < NUM_VERTICES; i++) { v2 = g.generateVertex(); v2.num = i; g.generateEdge(v1, v2); v1 = v2; } // Generate second layer foreach(v1; g.getVertices()) { v2 = g.generateVertex(); v2.num = i++; g.generateEdge(v1, v2); } // Initialize states foreach(v; g.getVertices()) v.state = NEW; // Run dfs algorithm (pass any vertex) dfs(first); }
Closing vertex: 0 Closing vertex: 1 Closing vertex: 2 Closing vertex: 3 Closing vertex: 11 Closing vertex: 4 Closing vertex: 5 Closing vertex: 6 Closing vertex: 7 Closing vertex: 17 Closing vertex: 8 Closing vertex: 9 Closing vertex: 18 Closing vertex: 16 Closing vertex: 12 Closing vertex: 15 Closing vertex: 19 Closing vertex: 10 Closing vertex: 14 Closing vertex: 13
Load a graph from a file and print its adjacency matrix.
function displayAdjacencyMatrix(matrix, vertices) { echo(" |"); foreach(vertex; vertices.iterator()) echo("" + vertex.__id + " "); echo("\n--+-------\n"); it = vertices.iterator(); foreach(line; matrix.iterator()) { if(it.hasNext()) vertex = it.next(); echo("" + vertex.__id + " |"); foreach(item; line.iterator()) echo("" + item + " "); echo("\n"); } } function main(argv) { g = graph(); assert(g.loadFromFile("../graphs/00_simple.txt")); displayAdjacencyMatrix(g.getAdjacencyMatrix(), g.getVertices()); }
|1 3 0 2 --+------- 1 |0 1 1 1 3 |1 0 1 1 0 |1 1 0 1 2 |1 1 1 0