|
| 1 | +--- |
| 2 | +id: how-to-make-a-graph |
| 3 | +title: 'Making a Graph' |
| 4 | +tags: ['introduction', 'how-to', 'getting started'] |
| 5 | +--- |
| 6 | + |
| 7 | +### Requirements |
| 8 | +* C++17 or later |
| 9 | +* GCC compiler v7.3.0 OR MSVC compiler supporting C++17 or later |
| 10 | + |
| 11 | +## Creating the Graph |
| 12 | + |
| 13 | +At the heart of CXXGraph is the graph object. The graph object contains the nodes and edges to be worked upon. |
| 14 | + |
| 15 | +Syntax for creating the graph object: |
| 16 | + |
| 17 | +``` |
| 18 | +Graph<int> myGraph; |
| 19 | +``` |
| 20 | + |
| 21 | +Whether a graph is directed or undirected is controlled by the edges added, *not the graph object itself.* |
| 22 | + |
| 23 | +### Adding Nodes and Edges |
| 24 | + |
| 25 | +To begin using the graph, we require nodes to create edges between. Nodes are created individually. |
| 26 | + |
| 27 | +Here we create two nodes... |
| 28 | + |
| 29 | +``` |
| 30 | +CXXGraph::Node<int> node1("1", 1); |
| 31 | +CXXGraph::Node<int> node2("2", 2); |
| 32 | +``` |
| 33 | + |
| 34 | +...and then pair them together. |
| 35 | + |
| 36 | +``` |
| 37 | +std::pair<const CXXGraph::Node<int> *, const CXXGraph::Node<int> *> pairNode( |
| 38 | + &node1, &node2); |
| 39 | +``` |
| 40 | + |
| 41 | +These nodes are ready to have an edge created between them. |
| 42 | + |
| 43 | +**Undirected** |
| 44 | + |
| 45 | +``` |
| 46 | +CXXGraph::UndirectedEdge<int> edge(1, pairNode); |
| 47 | +``` |
| 48 | + |
| 49 | +**Directed** |
| 50 | + |
| 51 | +``` |
| 52 | +CXXGraph::DirectedEdge<int> edge(1, pairNode); |
| 53 | +``` |
| 54 | + |
| 55 | +With the edge created, add the edge to the graph object with the `addEdge` function. |
| 56 | + |
| 57 | +``` |
| 58 | +myGraph.addEdge(edge); |
| 59 | +``` |
| 60 | + |
| 61 | +*The graph type is determined by the edges added to it.* |
| 62 | + |
| 63 | +To explore the full possibilities of CXXGraph, you can find a rundown of every CXXGraph class, function, and object [here](https://rawcdn.githack.com/ZigRazor/CXXGraph/master/docs/html/annotated.html). |
| 64 | + |
| 65 | +## Manipulating the Graph Object |
| 66 | + |
| 67 | +CXXGraph comes packed with many useful algorithms for your graphs. To use an algorithm, simply apply it to the graph object. |
| 68 | + |
| 69 | +**Traversal Algorithm** |
| 70 | + |
| 71 | +The staple Depth First Search can be added to a graph like so: |
| 72 | +``` |
| 73 | +auto res = myGraph.depth_first_search(node1, node2); |
| 74 | +``` |
| 75 | + |
| 76 | +**Advanced Algorithm** |
| 77 | + |
| 78 | +Adding a more targeted algorithim is done the same way: |
| 79 | +``` |
| 80 | +auto res = myGraph.dijkstra(node1, node3); |
| 81 | +``` |
| 82 | + |
| 83 | +For a Minimum Spanning Tree algorithim such as Kruskal's, simply ensure the right args are sent to the graph function. In this case, none, as Kruskal's traverses the entire graph. |
| 84 | +``` |
| 85 | +auto mst = myGraph.kruskal(); |
| 86 | +``` |
| 87 | + |
| 88 | +A full selection of CXXGraph's Algorithms are available [here](https://zigrazor.github.io/CXXGraph/component-explanation/regular-algorithm). |
| 89 | + |
| 90 | + |
0 commit comments