Skip to content

GraphAddEdge

Description

Add a directed edge from one node to another. Both endpoints must be existing node ids in the same graph.

Parameters

Name Direction Description
g in,out Graph handle.
from in Source GraphNodeId.
to in Destination GraphNodeId.

Success

Returns true. The edge entry has been appended to the outgoing-neighbour list of from and to the reverse predecessor list of to; edge_count grows by one.

Failure

Returns false on allocation failure for either side of the adjacency entry. The graph is unchanged. A reference to a non-live from or to node id is a caller bug and aborts via LOG_FATAL.

Usage example (Cross-references)

Usage examples (Cross-references)
        GraphNodeId c = GraphAddNodeR(&graph, 30);
        GraphNode   node_b;
        GraphAddEdge(&graph, a, b);
        GraphAddEdge(&graph, a, c);
        GraphAddEdge(&graph, c, a);
        GraphNode   node_b;
        GraphAddEdge(&graph, a, b);
        GraphAddEdge(&graph, a, c);
        GraphAddEdge(&graph, c, a);
        GraphAddEdge(&graph, a, b);
        GraphAddEdge(&graph, a, c);
        GraphAddEdge(&graph, c, a);
    
        ValidateGraph(&graph);
        GraphNodeId blue  = GraphAddNodeR(&graph, "blue");
    
        GraphAddEdge(&graph, red, green);
        GraphAddEdge(&graph, green, blue);
    
        GraphAddEdge(&graph, red, green);
        GraphAddEdge(&graph, green, blue);
    
        bool result = GraphHasEdge(&graph, red, green);
        GraphNodeId b = GraphAddNodeR(&graph, 20);
    
        GraphAddEdge(&graph, a, b);
        (void)GraphPredecessorAt(&graph, a, 0);
        GraphNodeId b = GraphAddNodeR(&graph, 20);
    
        GraphAddEdge(&graph, a, b);
        (void)GraphNeighborAt(&graph, b, 0);
        GraphNodeId c = GraphAddNodeR(&graph, 30);
    
        GraphAddEdge(&graph, a, b);
        GraphAddEdge(&graph, b, c);
        GraphAddEdge(&graph, c, a);
    
        GraphAddEdge(&graph, a, b);
        GraphAddEdge(&graph, b, c);
        GraphAddEdge(&graph, c, a);
        GraphAddEdge(&graph, a, b);
        GraphAddEdge(&graph, b, c);
        GraphAddEdge(&graph, c, a);
    
        GraphForeachNode(&graph, node) {
        GraphNodeId c = GraphAddNodeR(&graph, 30);
    
        GraphAddEdge(&graph, a, b);
        GraphAddEdge(&graph, a, c);
        GraphAddEdge(&graph, b, c);
    
        GraphAddEdge(&graph, a, b);
        GraphAddEdge(&graph, a, c);
        GraphAddEdge(&graph, b, c);
        GraphAddEdge(&graph, a, b);
        GraphAddEdge(&graph, a, c);
        GraphAddEdge(&graph, b, c);
    
        GraphForeachNode(&graph, node) {
        GraphNodeId b = GraphAddNodeR(&graph, 20);
    
        GraphAddEdge(&graph, a, b);
    
        bool result = !GraphEdgeMarkedForRemoval(&graph, a, b);
        GraphNodeId c = GraphAddNodeR(&graph, 30);
    
        GraphAddEdge(&graph, a, b);
        GraphAddEdge(&graph, a, c);
    
        GraphAddEdge(&graph, a, b);
        GraphAddEdge(&graph, a, c);
    
        bool result = GraphMarkEdgeForRemoval(&graph, a, b);
        GraphNodeId a = GraphAddNodeR(&graph, 10);
    
        bool result = GraphAddEdge(&graph, a, a);
        result      = result && (GraphOutDegree(&graph, a) == 1);
        result      = result && (GraphInDegree(&graph, a) == 1);
        GraphNodeId d = GraphAddNodeR(&graph, 40);
    
        GraphAddEdge(&graph, a, b);
        GraphAddEdge(&graph, b, c);
        GraphAddEdge(&graph, d, b);
    
        GraphAddEdge(&graph, a, b);
        GraphAddEdge(&graph, b, c);
        GraphAddEdge(&graph, d, b);
        GraphAddEdge(&graph, a, b);
        GraphAddEdge(&graph, b, c);
        GraphAddEdge(&graph, d, b);
    
        bool result = GraphMarkEdgeForRemoval(&graph, a, b);
        u64         slot_index;
    
        result = result && GraphAddEdge(&graph, first_id, second_id);
        result = result && GraphAddEdge(&graph, second_id, third_id);
        result = result && GraphAddEdge(&graph, third_id, first_id);
    
        result = result && GraphAddEdge(&graph, first_id, second_id);
        result = result && GraphAddEdge(&graph, second_id, third_id);
        result = result && GraphAddEdge(&graph, third_id, first_id);
        result = result && GraphAddEdge(&graph, third_id, third_id);
        result = result && GraphAddEdge(&graph, first_id, second_id);
        result = result && GraphAddEdge(&graph, second_id, third_id);
        result = result && GraphAddEdge(&graph, third_id, first_id);
        result = result && GraphAddEdge(&graph, third_id, third_id);
        result = result && (GraphNodeVisit(GraphGetNode(&graph, first_id)) == 1);
        result = result && GraphAddEdge(&graph, second_id, third_id);
        result = result && GraphAddEdge(&graph, third_id, first_id);
        result = result && GraphAddEdge(&graph, third_id, third_id);
        result = result && (GraphNodeVisit(GraphGetNode(&graph, first_id)) == 1);
        result = result && GraphMarkNodeForDeletion(GraphGetNode(&graph, second_id));
    
    static bool test_graph_add_edge_dedup(void) {
        WriteFmt("Testing GraphAddEdge deduplication\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        GraphNodeId c = GraphAddNodeR(&graph, 3);
    
        bool result = GraphAddEdge(&graph, a, b);
        result      = result && GraphAddEdge(&graph, a, c);
        result      = result && !GraphAddEdge(&graph, a, b);
    
        bool result = GraphAddEdge(&graph, a, b);
        result      = result && GraphAddEdge(&graph, a, c);
        result      = result && !GraphAddEdge(&graph, a, b);
        result      = result && GraphEdgeCount(&graph) == 2;
        bool result = GraphAddEdge(&graph, a, b);
        result      = result && GraphAddEdge(&graph, a, c);
        result      = result && !GraphAddEdge(&graph, a, b);
        result      = result && GraphEdgeCount(&graph) == 2;
        result      = result && GraphOutDegree(&graph, a) == 2;
        GraphNodeId c = GraphAddNodeR(&graph, 3);
    
        bool result = GraphAddEdge(&graph, a, a);
        result      = result && GraphAddEdge(&graph, b, a);
        result      = result && GraphAddEdge(&graph, c, a);
    
        bool result = GraphAddEdge(&graph, a, a);
        result      = result && GraphAddEdge(&graph, b, a);
        result      = result && GraphAddEdge(&graph, c, a);
        result      = result && !GraphAddEdge(&graph, a, a);
        bool result = GraphAddEdge(&graph, a, a);
        result      = result && GraphAddEdge(&graph, b, a);
        result      = result && GraphAddEdge(&graph, c, a);
        result      = result && !GraphAddEdge(&graph, a, a);
        result      = result && (GraphEdgeCount(&graph) == 3);
        result      = result && GraphAddEdge(&graph, b, a);
        result      = result && GraphAddEdge(&graph, c, a);
        result      = result && !GraphAddEdge(&graph, a, a);
        result      = result && (GraphEdgeCount(&graph) == 3);
        result      = result && (GraphOutDegree(&graph, a) == 1);
        GraphNodeId echo  = city_add_intersection(&graph, &index, &s_echo, &alloc);
    
        GraphAddEdge(&graph, alpha, beta);
        GraphAddEdge(&graph, beta, gamma);
        GraphAddEdge(&graph, gamma, delta);
    
        GraphAddEdge(&graph, alpha, beta);
        GraphAddEdge(&graph, beta, gamma);
        GraphAddEdge(&graph, gamma, delta);
        GraphAddEdge(&graph, gamma, echo);
        GraphAddEdge(&graph, alpha, beta);
        GraphAddEdge(&graph, beta, gamma);
        GraphAddEdge(&graph, gamma, delta);
        GraphAddEdge(&graph, gamma, echo);
        GraphAddEdge(&graph, echo, beta);
        GraphAddEdge(&graph, beta, gamma);
        GraphAddEdge(&graph, gamma, delta);
        GraphAddEdge(&graph, gamma, echo);
        GraphAddEdge(&graph, echo, beta);
        GraphAddEdge(&graph, gamma, delta);
        GraphAddEdge(&graph, gamma, echo);
        GraphAddEdge(&graph, echo, beta);
    
        bool result = city_reachable(&graph, &index, &s_alpha, &s_delta);
        GraphNodeId d = GraphAddNodeR(&graph, 4);
    
        GraphAddEdge(&graph, a, b);
        GraphAddEdge(&graph, a, c);
        GraphAddEdge(&graph, b, d);
    
        GraphAddEdge(&graph, a, b);
        GraphAddEdge(&graph, a, c);
        GraphAddEdge(&graph, b, d);
        GraphAddEdge(&graph, c, d);
        GraphAddEdge(&graph, a, b);
        GraphAddEdge(&graph, a, c);
        GraphAddEdge(&graph, b, d);
        GraphAddEdge(&graph, c, d);
        GraphAddEdge(&graph, a, c);
        GraphAddEdge(&graph, b, d);
        GraphAddEdge(&graph, c, d);
    
        GraphForeachNode(&graph, node) {
        GraphNodeId d = GraphAddNodeR(&graph, 4);
    
        GraphAddEdge(&graph, a, d);
        GraphAddEdge(&graph, b, d);
        GraphAddEdge(&graph, c, d);
    
        GraphAddEdge(&graph, a, d);
        GraphAddEdge(&graph, b, d);
        GraphAddEdge(&graph, c, d);
        GraphAddEdge(&graph, a, b);
        GraphAddEdge(&graph, a, d);
        GraphAddEdge(&graph, b, d);
        GraphAddEdge(&graph, c, d);
        GraphAddEdge(&graph, a, b);
        GraphAddEdge(&graph, b, d);
        GraphAddEdge(&graph, c, d);
        GraphAddEdge(&graph, a, b);
    
        u64 predecessor_sum   = 0;
        GraphNodeId c = GraphAddNodeR(&graph, 3);
    
        GraphAddEdge(&graph, a, b);
    
        GraphNodeForeachNeighbor(GraphGetNode(&graph, a), neighbor) {
        GraphNodeForeachNeighbor(GraphGetNode(&graph, a), neighbor) {
            (void)neighbor;
            (void)GraphAddEdge(&graph, a, c);
        }
        GraphNodeId d = GraphAddNodeR(&graph, 4);
    
        GraphAddEdge(&graph, a, c);
        GraphAddEdge(&graph, b, c);
    
        GraphAddEdge(&graph, a, c);
        GraphAddEdge(&graph, b, c);
    
        GraphNodeForeachPredecessor(GraphGetNode(&graph, c), predecessor) {
        GraphNodeForeachPredecessor(GraphGetNode(&graph, c), predecessor) {
            (void)predecessor;
            (void)GraphAddEdge(&graph, d, c);
        }
    #define GraphMustAddEdge(g, from, to)                                                                                  \
        do {                                                                                                               \
            if (!GraphAddEdge((g), (from), (to))) {                                                                        \
                LOG_FATAL("GraphMustAddEdge failed");                                                                      \
            }                                                                                                              \
Last updated on