Graph::addEdge

Function


void addEdge(const T& x, const T& y);
void addEdge(const T& x, const T& y, const double & w);
void addEdge(Edge<T> e);

Add a new edge into the directed graph to connected from node x to node y, with weight w. Default value of weight is 1. Weight of the edge must be positive.

Parameters

x, y, are label of the vertices to be connected by the new edge.
x and y must exist in the graph, otherwise the edge will not be add.
w is the weight of the edge. Default value of weight is 1.

Return value

None

Example

#include "HKUAL_graph.h"
#include <iostream>
#include <vector>
using namespace std;
using namespace HKUAL;

int main(){
    DirectedGraph<string> g;
    g.addVertex("A");
    g.addVertex("B");
    g.addVertex("C");
    g.addEdge("A", "B");
    g.addEdge("A", "C", 2);
    g.addEdge("B", "C", 10);
    
    return 0;
}

Output

None

Time Complexity

, for is number of vertices in the graph.

© The University of Hong Kong Algorithms Library - hkual@cs.hku.hk