GraphTheory - Maple Programming Help

Home : Support : Online Help : Mathematics : Discrete Mathematics : Graph Theory : GraphTheory Package : GraphTheory/GraphUnion

GraphTheory

 GraphUnion

 Calling Sequence GraphUnion(G1,...,Gs)

Parameters

 G1,...,Gs - graphs

Description

 • The GraphUnion function returns a graph G which is the union of the graphs G1,...,Gs, such that

$\mathrm{Vertices}\left(G\right)=\mathrm{Vertices}\left(\mathrm{G1}\right)\cup \cdots \cup \mathrm{Vertices}\left(\mathrm{Gs}\right)$

$\mathrm{Edges}\left(G\right)=\mathrm{Edges}\left(\mathrm{G1}\right)\cup \cdots \cup \mathrm{Edges}\left(\mathrm{Gs}\right)$

 • Note that the graphs G1,...,Gs must all be directed or all undirected, and the resulting graph is directed or undirected, respectively. Likewise, the graphs G1,...,Gs must all be weighted or all unweighted, and the resulting graph is then weighted or unweighted, respectively.
 • Moreover, if G1,...,Gs are all weighted graphs, the resulting graph is a weighted graph where the weight of any common edge is the sum of the weights of that edge in G1,...,Gs.

Examples

 > with(GraphTheory):
 > G1 := Graph(directed,[1,2,3],{[1,2],[2,3]});
 ${\mathrm{G1}}{≔}{\mathrm{Graph 1: a directed unweighted graph with 3 vertices and 2 arc\left(s\right)}}$ (1)
 > G2 := Graph(directed,[1,2,3],{[3,1],[2,3]});
 ${\mathrm{G2}}{≔}{\mathrm{Graph 2: a directed unweighted graph with 3 vertices and 2 arc\left(s\right)}}$ (2)
 > G := GraphUnion(G1,G2);
 ${G}{≔}{\mathrm{Graph 3: a directed unweighted graph with 3 vertices and 3 arc\left(s\right)}}$ (3)
 > Vertices(G);
 $\left[{1}{,}{2}{,}{3}\right]$ (4)
 > Edges(G);
 $\left\{\left[{1}{,}{2}\right]{,}\left[{2}{,}{3}\right]{,}\left[{3}{,}{1}\right]\right\}$ (5)

In this example, note that the vertices of G1 and G2 are permuted differently.

 > G1 := Graph([a,b,c],{[{a,b},3],[{b,c},4]});
 ${\mathrm{G1}}{≔}{\mathrm{Graph 4: an undirected weighted graph with 3 vertices and 2 edge\left(s\right)}}$ (6)
 > G2 := Graph([a,c,b],{[{a,c},5],[{b,c},6]});
 ${\mathrm{G2}}{≔}{\mathrm{Graph 5: an undirected weighted graph with 3 vertices and 2 edge\left(s\right)}}$ (7)
 > G := GraphUnion(G1,G2);
 ${G}{≔}{\mathrm{Graph 6: an undirected weighted graph with 3 vertices and 3 edge\left(s\right)}}$ (8)
 > Vertices(G);
 $\left[{a}{,}{b}{,}{c}\right]$ (9)
 > Edges(G);
 $\left\{\left\{{a}{,}{b}\right\}{,}\left\{{a}{,}{c}\right\}{,}\left\{{b}{,}{c}\right\}\right\}$ (10)
 > WeightMatrix(G1), WeightMatrix(G2), WeightMatrix(G);
 $\left[\begin{array}{ccc}{0}& {3}& {0}\\ {3}& {0}& {4}\\ {0}& {4}& {0}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {5}& {0}\\ {5}& {0}& {6}\\ {0}& {6}& {0}\end{array}\right]{,}\left[\begin{array}{ccc}{0}& {3}& {5}\\ {3}& {0}& {10}\\ {5}& {10}& {0}\end{array}\right]$ (11)

In this example, note that there is one common vertex between the two graphs

 > G1 := Graph(["a","b"],{{"a","b"}});
 ${\mathrm{G1}}{≔}{\mathrm{Graph 7: an undirected unweighted graph with 2 vertices and 1 edge\left(s\right)}}$ (12)
 > G2 := Graph(["a","c"],{{"a","c"}});
 ${\mathrm{G2}}{≔}{\mathrm{Graph 8: an undirected unweighted graph with 2 vertices and 1 edge\left(s\right)}}$ (13)
 > G := GraphUnion(G1,G2);
 ${G}{≔}{\mathrm{Graph 9: an undirected unweighted graph with 3 vertices and 2 edge\left(s\right)}}$ (14)
 > Vertices(G);
 $\left[{"a"}{,}{"b"}{,}{"c"}\right]$ (15)
 > Edges(G);
 $\left\{\left\{{"a"}{,}{"b"}\right\}{,}\left\{{"a"}{,}{"c"}\right\}\right\}$ (16)