Wolfram Mathematica in Graph Theory

Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Wolfram Mathematica in Graph Theory
Marek Majewski
Last updated: May 16, 2016
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Mathematica provides many functions for constructing combinatorical
objects such as permutations and subsets.
To load the Combinatorica package write
« Combinatorica‘
Permutations[list] generates a list of all possible permutations of the
elements in list.
Permutations[{a,b,c,d,1}]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Mathematica provides many functions for constructing combinatorical
objects such as permutations and subsets.
To load the Combinatorica package write
« Combinatorica‘
Permutations[list] generates a list of all possible permutations of the
elements in list.
Permutations[{a,b,c,d,1}]
KSubsets[l,k] gives all subsets of set l containing exactly k elements,
ordered lexicographically.
KSubsets[{1, 2, 3, 4, 5}, 3]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
There are many typical graphs which are predefined in Combinatorica
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
There are many typical graphs which are predefined in Combinatorica
To dispalay graph write ShowGraph[]:
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
There are many typical graphs which are predefined in Combinatorica
To dispalay graph write ShowGraph[]:
ShowGraph[CompleteGraph[6]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
There are many typical graphs which are predefined in Combinatorica
To dispalay graph write ShowGraph[]:
ShowGraph[CompleteGraph[6]]
ShowGraph[RandomTree[10]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
There are many typical graphs which are predefined in Combinatorica
To dispalay graph write ShowGraph[]:
ShowGraph[CompleteGraph[6]]
ShowGraph[RandomTree[10]]
ShowGraph[Hypercube[3]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
There are many typical graphs which are predefined in Combinatorica
To dispalay graph write ShowGraph[]:
ShowGraph[CompleteGraph[6]]
ShowGraph[RandomTree[10]]
ShowGraph[Hypercube[3]]
To get an information about the number of edges and vertices it is
enough to write
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
There are many typical graphs which are predefined in Combinatorica
To dispalay graph write ShowGraph[]:
ShowGraph[CompleteGraph[6]]
ShowGraph[RandomTree[10]]
ShowGraph[Hypercube[3]]
To get an information about the number of edges and vertices it is
enough to write
CompleteGraph[6]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
There are many typical graphs which are predefined in Combinatorica
To dispalay graph write ShowGraph[]:
ShowGraph[CompleteGraph[6]]
ShowGraph[RandomTree[10]]
ShowGraph[Hypercube[3]]
To get an information about the number of edges and vertices it is
enough to write
CompleteGraph[6]
Also you can use the commands
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
There are many typical graphs which are predefined in Combinatorica
To dispalay graph write ShowGraph[]:
ShowGraph[CompleteGraph[6]]
ShowGraph[RandomTree[10]]
ShowGraph[Hypercube[3]]
To get an information about the number of edges and vertices it is
enough to write
CompleteGraph[6]
Also you can use the commands
V[CompleteGraph[6]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
There are many typical graphs which are predefined in Combinatorica
To dispalay graph write ShowGraph[]:
ShowGraph[CompleteGraph[6]]
ShowGraph[RandomTree[10]]
ShowGraph[Hypercube[3]]
To get an information about the number of edges and vertices it is
enough to write
CompleteGraph[6]
Also you can use the commands
V[CompleteGraph[6]]
M[CompleteGraph[6]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Selected predefined graphs
CompleteGraph[numb-vert]
CompleteBinaryTree[numb-vert]
CompleteKPartiteGraph[numb-vert]
RegularGraph[deg,numb-vert]
Cycle[numb-vert]
Wheel[numb-vert]
DeBruijnGraph[numb-symb,dim]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
A variety of options that control the appearance can be associated with
ShowGraph[] .
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
A variety of options that control the appearance can be associated with
ShowGraph[] .
ShowGraph[CompleteGraph[5],VertexColor->Green,EdgeColor->Blue,
VertexStyle->Disk[Large]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
A variety of options that control the appearance can be associated with
ShowGraph[] .
ShowGraph[CompleteGraph[5],VertexColor->Green,EdgeColor->Blue,
VertexStyle->Disk[Large]]
The general syntax of ShowGraph[] is:
ShowGraph[g,options]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
A variety of options that control the appearance can be associated with
ShowGraph[] .
ShowGraph[CompleteGraph[5],VertexColor->Green,EdgeColor->Blue,
VertexStyle->Disk[Large]]
The general syntax of ShowGraph[] is:
ShowGraph[g,options]
Selected options of ShowGraph[]
VertexColor->kolor
a color from the package Graphics‘Colors‘
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
A variety of options that control the appearance can be associated with
ShowGraph[] .
ShowGraph[CompleteGraph[5],VertexColor->Green,EdgeColor->Blue,
VertexStyle->Disk[Large]]
The general syntax of ShowGraph[] is:
ShowGraph[g,options]
Selected options of ShowGraph[]
VertexColor->kolor
a color from the package Graphics‘Colors‘
VertexStyle->X[y]
X=Disk, Box Y=Small, Normal, Large, positive-number
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
A variety of options that control the appearance can be associated with
ShowGraph[] .
ShowGraph[CompleteGraph[5],VertexColor->Green,EdgeColor->Blue,
VertexStyle->Disk[Large]]
The general syntax of ShowGraph[] is:
ShowGraph[g,options]
Selected options of ShowGraph[]
VertexColor->kolor
a color from the package Graphics‘Colors‘
VertexStyle->X[y]
X=Disk, Box Y=Small, Normal, Large, positive-number
VertexNumber->On/Off
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
A variety of options that control the appearance can be associated with
ShowGraph[] .
ShowGraph[CompleteGraph[5],VertexColor->Green,EdgeColor->Blue,
VertexStyle->Disk[Large]]
The general syntax of ShowGraph[] is:
ShowGraph[g,options]
Selected options of ShowGraph[]
VertexColor->kolor
a color from the package Graphics‘Colors‘
VertexStyle->X[y]
X=Disk, Box Y=Small, Normal, Large, positive-number
VertexNumber->On/Off
VertexNumberColor->color
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
A variety of options that control the appearance can be associated with
ShowGraph[] .
ShowGraph[CompleteGraph[5],VertexColor->Green,EdgeColor->Blue,
VertexStyle->Disk[Large]]
The general syntax of ShowGraph[] is:
ShowGraph[g,options]
Selected options of ShowGraph[]
VertexColor->kolor
a color from the package Graphics‘Colors‘
VertexStyle->X[y]
X=Disk, Box Y=Small, Normal, Large, positive-number
VertexNumber->On/Off
VertexNumberColor->color
VertexNumberPosition->X
X=Center, LowerLeft, UpperRight, LowerRight, UpperLeft or {x,y}
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
A variety of options that control the appearance can be associated with
ShowGraph[] .
ShowGraph[CompleteGraph[5],VertexColor->Green,EdgeColor->Blue,
VertexStyle->Disk[Large]]
The general syntax of ShowGraph[] is:
ShowGraph[g,options]
Selected options of ShowGraph[]
VertexColor->kolor
a color from the package Graphics‘Colors‘
VertexStyle->X[y]
X=Disk, Box Y=Small, Normal, Large, positive-number
VertexNumber->On/Off
VertexNumberColor->color
VertexNumberPosition->X
X=Center, LowerLeft, UpperRight, LowerRight, UpperLeft or {x,y}
VertexLabel->X
X=False or a list of label in order of vertices
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
A variety of options that control the appearance can be associated with
ShowGraph[] .
ShowGraph[CompleteGraph[5],VertexColor->Green,EdgeColor->Blue,
VertexStyle->Disk[Large]]
The general syntax of ShowGraph[] is:
ShowGraph[g,options]
Selected options of ShowGraph[]
VertexColor->kolor
a color from the package Graphics‘Colors‘
VertexStyle->X[y]
X=Disk, Box Y=Small, Normal, Large, positive-number
VertexNumber->On/Off
VertexNumberColor->color
VertexNumberPosition->X
X=Center, LowerLeft, UpperRight, LowerRight, UpperLeft or {x,y}
VertexLabel->X
X=False or a list of label in order of vertices
VertexLabelColor->color
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
A variety of options that control the appearance can be associated with
ShowGraph[] .
ShowGraph[CompleteGraph[5],VertexColor->Green,EdgeColor->Blue,
VertexStyle->Disk[Large]]
The general syntax of ShowGraph[] is:
ShowGraph[g,options]
Selected options of ShowGraph[]
VertexColor->kolor
a color from the package Graphics‘Colors‘
VertexStyle->X[y]
X=Disk, Box Y=Small, Normal, Large, positive-number
VertexNumber->On/Off
VertexNumberColor->color
VertexNumberPosition->X
X=Center, LowerLeft, UpperRight, LowerRight, UpperLeft or {x,y}
VertexLabel->X
X=False or a list of label in order of vertices
VertexLabelColor->color
VertexLabelPosition->X
X=Center, LowerLeft, UpperRight, LowerRight, UpperLeft or {x,y}
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Selected options of ShowGraph[] – cont.
EdgeColor->kolor
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Selected options of ShowGraph[] – cont.
EdgeColor->kolor
EdgeStyle->X
X=Thick, Normal, Thin, ThickDashed, NormalDashed, ThinDashed
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Selected options of ShowGraph[] – cont.
EdgeColor->kolor
EdgeStyle->X
X=Thick, Normal, Thin, ThickDashed, NormalDashed, ThinDashed
EdgeLabel->X
X=False or a list of label in order of vertices
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Selected options of ShowGraph[] – cont.
EdgeColor->kolor
EdgeStyle->X
X=Thick, Normal, Thin, ThickDashed, NormalDashed, ThinDashed
EdgeLabel->X
X=False or a list of label in order of vertices
EdgeLabelColor->color
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Selected options of ShowGraph[] – cont.
EdgeColor->kolor
EdgeStyle->X
X=Thick, Normal, Thin, ThickDashed, NormalDashed, ThinDashed
EdgeLabel->X
X=False or a list of label in order of vertices
EdgeLabelColor->color
EdgeLabelPosition->X
X=Center, LowerLeft, UpperRight, LowerRight, UpperLeft or {x,y}
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Selected options of ShowGraph[] – cont.
EdgeColor->kolor
EdgeStyle->X
X=Thick, Normal, Thin, ThickDashed, NormalDashed, ThinDashed
EdgeLabel->X
X=False or a list of label in order of vertices
EdgeLabelColor->color
EdgeLabelPosition->X
X=Center, LowerLeft, UpperRight, LowerRight, UpperLeft or {x,y}
EdgeDirection->True/False
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Selected options of ShowGraph[] – cont.
EdgeColor->kolor
EdgeStyle->X
X=Thick, Normal, Thin, ThickDashed, NormalDashed, ThinDashed
EdgeLabel->X
X=False or a list of label in order of vertices
EdgeLabelColor->color
EdgeLabelPosition->X
X=Center, LowerLeft, UpperRight, LowerRight, UpperLeft or {x,y}
EdgeDirection->True/False
It is possible to use the above option for a specified vertices
ShowGraph[CompleteGraph[5],{{1,2,VertexColor->Green,
VertexNumber->On},{3,4,5,VertexColor->Blue}},
VertexStyle->Disk[Large],EdgeColor->Red,
EdgeLabel->{"A","B","C","D","E","F","G","H","I","J","K","L"}]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Different drawings or embeddings of a graph can reveal different aspects
of its structure.
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Different drawings or embeddings of a graph can reveal different aspects
of its structure.
CircularEmbedding[graph] – vertices displayed on a circle
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Different drawings or embeddings of a graph can reveal different aspects
of its structure.
CircularEmbedding[graph] – vertices displayed on a circle
ShowGraphArray[{g1=CompleteBinaryTree[5],
g2=CompleteKPartiteGraph[2,5], g3=MycielskiGraph[4],
CircularEmbedding[g1], CircularEmbedding[g2],
CircularEmbedding[g3]}]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Different drawings or embeddings of a graph can reveal different aspects
of its structure.
CircularEmbedding[graph] – vertices displayed on a circle
ShowGraphArray[{g1=CompleteBinaryTree[5],
g2=CompleteKPartiteGraph[2,5], g3=MycielskiGraph[4],
CircularEmbedding[g1], CircularEmbedding[g2],
CircularEmbedding[g3]}]
RankedEmbedding[graph,set-of-lists] – vertices are placed on
distinct vertical lines
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Different drawings or embeddings of a graph can reveal different aspects
of its structure.
CircularEmbedding[graph] – vertices displayed on a circle
ShowGraphArray[{g1=CompleteBinaryTree[5],
g2=CompleteKPartiteGraph[2,5], g3=MycielskiGraph[4],
CircularEmbedding[g1], CircularEmbedding[g2],
CircularEmbedding[g3]}]
RankedEmbedding[graph,set-of-lists] – vertices are placed on
distinct vertical lines
p={{1,2,3},{4,5}}
ShowGraphArray[{g4=CompleteGraph[5],RankedEmbedding[g4,p]},
VertexNumber->On]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Different drawings or embeddings of a graph can reveal different aspects
of its structure.
CircularEmbedding[graph] – vertices displayed on a circle
ShowGraphArray[{g1=CompleteBinaryTree[5],
g2=CompleteKPartiteGraph[2,5], g3=MycielskiGraph[4],
CircularEmbedding[g1], CircularEmbedding[g2],
CircularEmbedding[g3]}]
RankedEmbedding[graph,set-of-lists] – vertices are placed on
distinct vertical lines
p={{1,2,3},{4,5}}
ShowGraphArray[{g4=CompleteGraph[5],RankedEmbedding[g4,p]},
VertexNumber->On]
RadialEmbedding[graph,vert] – a given vertex vert is placed on the
center of a circle with the rest of vertices placed on concentric circles
around the center
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Different drawings or embeddings of a graph can reveal different aspects
of its structure.
CircularEmbedding[graph] – vertices displayed on a circle
ShowGraphArray[{g1=CompleteBinaryTree[5],
g2=CompleteKPartiteGraph[2,5], g3=MycielskiGraph[4],
CircularEmbedding[g1], CircularEmbedding[g2],
CircularEmbedding[g3]}]
RankedEmbedding[graph,set-of-lists] – vertices are placed on
distinct vertical lines
p={{1,2,3},{4,5}}
ShowGraphArray[{g4=CompleteGraph[5],RankedEmbedding[g4,p]},
VertexNumber->On]
RadialEmbedding[graph,vert] – a given vertex vert is placed on the
center of a circle with the rest of vertices placed on concentric circles
around the center
ShowGraphArray[{g1,g4=CompleteGraph[5],RadialEmbedding[g1,1],
RadialEmbedding[g4,1]},VertexNumber->On]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
RootedEmbedding[graph,vert] – hierarchic structure with the
specified root vert
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
RootedEmbedding[graph,vert] – hierarchic structure with the
specified root vert
ShowGraphArray[{g1,g2,g3,
RootedEmbedding[g1,1],RootedEmbedding[g2,1],
RootedEmbedding[g3,1]}]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
RootedEmbedding[graph,vert] – hierarchic structure with the
specified root vert
ShowGraphArray[{g1,g2,g3,
RootedEmbedding[g1,1],RootedEmbedding[g2,1],
RootedEmbedding[g3,1]}]
SpringEmbedding[graph] models the graphs as a system of springs
and lets the Hooke’s law space the vertices.
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
RootedEmbedding[graph,vert] – hierarchic structure with the
specified root vert
ShowGraphArray[{g1,g2,g3,
RootedEmbedding[g1,1],RootedEmbedding[g2,1],
RootedEmbedding[g3,1]}]
SpringEmbedding[graph] models the graphs as a system of springs
and lets the Hooke’s law space the vertices.
ShowGraphArray[{g1,g2,g3,
SpringEmbedding[g1],SpringEmbedding[g2],SpringEmbedding[g3]}]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Edge list
Edge list Edges[] is the simplest way of graph representantion.
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Edge list
Edge list Edges[] is the simplest way of graph representantion.
g=Star[7]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Edge list
Edge list Edges[] is the simplest way of graph representantion.
g=Star[7]
Edges[g]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Edge list
Edge list Edges[] is the simplest way of graph representantion.
g=Star[7]
Edges[g]
Edges[g,EdgeWeight]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Edge list
Edge list Edges[] is the simplest way of graph representantion.
g=Star[7]
Edges[g]
Edges[g,EdgeWeight]
We can define a graph from the edge list by the commands
FromOrderedPairs[] and FromUnorderedPairs[]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Edge list
Edge list Edges[] is the simplest way of graph representantion.
g=Star[7]
Edges[g]
Edges[g,EdgeWeight]
We can define a graph from the edge list by the commands
FromOrderedPairs[] and FromUnorderedPairs[]
p={{1,2},{1,3},{2,3}}
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Edge list
Edge list Edges[] is the simplest way of graph representantion.
g=Star[7]
Edges[g]
Edges[g,EdgeWeight]
We can define a graph from the edge list by the commands
FromOrderedPairs[] and FromUnorderedPairs[]
p={{1,2},{1,3},{2,3}}
g=FromUnorderedPairs[p]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Edge list
Edge list Edges[] is the simplest way of graph representantion.
g=Star[7]
Edges[g]
Edges[g,EdgeWeight]
We can define a graph from the edge list by the commands
FromOrderedPairs[] and FromUnorderedPairs[]
p={{1,2},{1,3},{2,3}}
g=FromUnorderedPairs[p]
ShowGraph[g]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Edge list
Edge list Edges[] is the simplest way of graph representantion.
g=Star[7]
Edges[g]
Edges[g,EdgeWeight]
We can define a graph from the edge list by the commands
FromOrderedPairs[] and FromUnorderedPairs[]
p={{1,2},{1,3},{2,3}}
g=FromUnorderedPairs[p]
ShowGraph[g]
ShowGraph[FromOrderedPairs]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Adjacency list
The adjacency list consists of n list, one for each vertex, which records
the vertices to which a given vertex is adjacent. This is realizing by two
commands: ToAdjacencyLists[graph] and
FromAdjacencyLists[list]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Adjacency list
The adjacency list consists of n list, one for each vertex, which records
the vertices to which a given vertex is adjacent. This is realizing by two
commands: ToAdjacencyLists[graph] and
FromAdjacencyLists[list]
ToAdjacencyLists[g=Cycle[7]]
We can use the commands ToAdjacencyLists[graph] with the option
//ColumnForm
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Adjacency list
The adjacency list consists of n list, one for each vertex, which records
the vertices to which a given vertex is adjacent. This is realizing by two
commands: ToAdjacencyLists[graph] and
FromAdjacencyLists[list]
ToAdjacencyLists[g=Cycle[7]]
We can use the commands ToAdjacencyLists[graph] with the option
//ColumnForm
ToAdjacencyLists[g]//ColumnForm
The command FromAdjacencyLists[list] is very simple
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Adjacency list
The adjacency list consists of n list, one for each vertex, which records
the vertices to which a given vertex is adjacent. This is realizing by two
commands: ToAdjacencyLists[graph] and
FromAdjacencyLists[list]
ToAdjacencyLists[g=Cycle[7]]
We can use the commands ToAdjacencyLists[graph] with the option
//ColumnForm
ToAdjacencyLists[g]//ColumnForm
The command FromAdjacencyLists[list] is very simple
l={{2,3,4},{1,3,4},{1,2,4},{2}}
ShowGraph[FromAdjacencyLists[l],VertexNumber->On]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Adjacency list
The adjacency list consists of n list, one for each vertex, which records
the vertices to which a given vertex is adjacent. This is realizing by two
commands: ToAdjacencyLists[graph] and
FromAdjacencyLists[list]
ToAdjacencyLists[g=Cycle[7]]
We can use the commands ToAdjacencyLists[graph] with the option
//ColumnForm
ToAdjacencyLists[g]//ColumnForm
The command FromAdjacencyLists[list] is very simple
l={{2,3,4},{1,3,4},{1,2,4},{2}}
ShowGraph[FromAdjacencyLists[l],VertexNumber->On]
We can use the above command also to represent a graph with
multiedges na loops
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Adjacency list
The adjacency list consists of n list, one for each vertex, which records
the vertices to which a given vertex is adjacent. This is realizing by two
commands: ToAdjacencyLists[graph] and
FromAdjacencyLists[list]
ToAdjacencyLists[g=Cycle[7]]
We can use the commands ToAdjacencyLists[graph] with the option
//ColumnForm
ToAdjacencyLists[g]//ColumnForm
The command FromAdjacencyLists[list] is very simple
l={{2,3,4},{1,3,4},{1,2,4},{2}}
ShowGraph[FromAdjacencyLists[l],VertexNumber->On]
We can use the above command also to represent a graph with
multiedges na loops
ShowGraph[FromAdjacencyLists[{{1,2,2},{2,1,3},{2}}]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Adjacency matrix
We can use the commands ToAdjacencyMatrix[graf] and
FromAdjacencyMatrix[macierz] in an analogous method
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Adjacency matrix
We can use the commands ToAdjacencyMatrix[graf] and
FromAdjacencyMatrix[macierz] in an analogous method
ToAdjacencyMatrix[CompleteGraph[5]] // TableForm
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Adjacency matrix
We can use the commands ToAdjacencyMatrix[graf] and
FromAdjacencyMatrix[macierz] in an analogous method
ToAdjacencyMatrix[CompleteGraph[5]] // TableForm
m={{1,1,1},{1,1,1},{1,1,1}}
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Adjacency matrix
We can use the commands ToAdjacencyMatrix[graf] and
FromAdjacencyMatrix[macierz] in an analogous method
ToAdjacencyMatrix[CompleteGraph[5]] // TableForm
m={{1,1,1},{1,1,1},{1,1,1}}
ShowGraph[FromAdjacencyMatrix[m]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
Adjacency matrix
We can use the commands ToAdjacencyMatrix[graf] and
FromAdjacencyMatrix[macierz] in an analogous method
ToAdjacencyMatrix[CompleteGraph[5]] // TableForm
m={{1,1,1},{1,1,1},{1,1,1}}
ShowGraph[FromAdjacencyMatrix[m]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
DegreeSequence
To sum up this topic let us check the command
DegreeSequence[graph]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
DegreeSequence
To sum up this topic let us check the command
DegreeSequence[graph]
DegreeSequence[g=RandomTree[5]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
DegreeSequence
To sum up this topic let us check the command
DegreeSequence[graph]
DegreeSequence[g=RandomTree[5]]
ShowGraph[g]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
DegreeSequence
To sum up this topic let us check the command
DegreeSequence[graph]
DegreeSequence[g=RandomTree[5]]
ShowGraph[g]
In fact, DegreeSequence in Combinatorica denotes the sequence of degrees
of consecutive vertices.
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
DegreeSequence
To sum up this topic let us check the command
DegreeSequence[graph]
DegreeSequence[g=RandomTree[5]]
ShowGraph[g]
In fact, DegreeSequence in Combinatorica denotes the sequence of degrees
of consecutive vertices.
We can define a graph by DegreeSequence, that is with the aid of
RealizeDegreeSequence[list]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
DegreeSequence
To sum up this topic let us check the command
DegreeSequence[graph]
DegreeSequence[g=RandomTree[5]]
ShowGraph[g]
In fact, DegreeSequence in Combinatorica denotes the sequence of degrees
of consecutive vertices.
We can define a graph by DegreeSequence, that is with the aid of
RealizeDegreeSequence[list]
ShowGraph[g=RealizeDegreeSequence[{3,2,1,1,1}]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Basic instructions
Graphical representations of graphs
Computer representation of graphs
DegreeSequence
To sum up this topic let us check the command
DegreeSequence[graph]
DegreeSequence[g=RandomTree[5]]
ShowGraph[g]
In fact, DegreeSequence in Combinatorica denotes the sequence of degrees
of consecutive vertices.
We can define a graph by DegreeSequence, that is with the aid of
RealizeDegreeSequence[list]
ShowGraph[g=RealizeDegreeSequence[{3,2,1,1,1}]]
Exercise
Draw the graphs defined by the degree sequence, but in the sense of
definition given in the lecture:
(a) 4, 4, 3, 2, 2, 1,
(b) 4, 4, 4, 3, 2, 1,
(c) 5, 5, 4, 3, 3, 2.
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Adding vertices and edges
To add the list list of edges to the existing graph graph we use the
command AddEdges[graph,list]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Adding vertices and edges
To add the list list of edges to the existing graph graph we use the
command AddEdges[graph,list]
ShowGraph[g=Star[10],VertexNumber->On]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Adding vertices and edges
To add the list list of edges to the existing graph graph we use the
command AddEdges[graph,list]
ShowGraph[g=Star[10],VertexNumber->On]
g1=AddEdges[g,{{1,2},{5,7},{5,7},{9,9}}]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Adding vertices and edges
To add the list list of edges to the existing graph graph we use the
command AddEdges[graph,list]
ShowGraph[g=Star[10],VertexNumber->On]
g1=AddEdges[g,{{1,2},{5,7},{5,7},{9,9}}]
ShowGraph[g1,VertexNumber->On]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Adding vertices and edges
To add the list list of edges to the existing graph graph we use the
command AddEdges[graph,list]
ShowGraph[g=Star[10],VertexNumber->On]
g1=AddEdges[g,{{1,2},{5,7},{5,7},{9,9}}]
ShowGraph[g1,VertexNumber->On]
ShowGraph[g2=AddEdges[g1,Edges[Cycle[9]]],VertexNumber->On]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Adding vertices and edges
To add the list list of edges to the existing graph graph we use the
command AddEdges[graph,list]
ShowGraph[g=Star[10],VertexNumber->On]
g1=AddEdges[g,{{1,2},{5,7},{5,7},{9,9}}]
ShowGraph[g1,VertexNumber->On]
ShowGraph[g2=AddEdges[g1,Edges[Cycle[9]]],VertexNumber->On]
In a very similar way we add (isolated) vertices
AddVertices[graph,vert]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Adding vertices and edges
To add the list list of edges to the existing graph graph we use the
command AddEdges[graph,list]
ShowGraph[g=Star[10],VertexNumber->On]
g1=AddEdges[g,{{1,2},{5,7},{5,7},{9,9}}]
ShowGraph[g1,VertexNumber->On]
ShowGraph[g2=AddEdges[g1,Edges[Cycle[9]]],VertexNumber->On]
In a very similar way we add (isolated) vertices
AddVertices[graph,vert]
As an argument we can use the number of vertices
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Adding vertices and edges
To add the list list of edges to the existing graph graph we use the
command AddEdges[graph,list]
ShowGraph[g=Star[10],VertexNumber->On]
g1=AddEdges[g,{{1,2},{5,7},{5,7},{9,9}}]
ShowGraph[g1,VertexNumber->On]
ShowGraph[g2=AddEdges[g1,Edges[Cycle[9]]],VertexNumber->On]
In a very similar way we add (isolated) vertices
AddVertices[graph,vert]
As an argument we can use the number of vertices
ShowGraph[g2=AddVertices[g1,4]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Adding vertices and edges
To add the list list of edges to the existing graph graph we use the
command AddEdges[graph,list]
ShowGraph[g=Star[10],VertexNumber->On]
g1=AddEdges[g,{{1,2},{5,7},{5,7},{9,9}}]
ShowGraph[g1,VertexNumber->On]
ShowGraph[g2=AddEdges[g1,Edges[Cycle[9]]],VertexNumber->On]
In a very similar way we add (isolated) vertices
AddVertices[graph,vert]
As an argument we can use the number of vertices
ShowGraph[g2=AddVertices[g1,4]]
and the specified cordinates of vertices
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Adding vertices and edges
To add the list list of edges to the existing graph graph we use the
command AddEdges[graph,list]
ShowGraph[g=Star[10],VertexNumber->On]
g1=AddEdges[g,{{1,2},{5,7},{5,7},{9,9}}]
ShowGraph[g1,VertexNumber->On]
ShowGraph[g2=AddEdges[g1,Edges[Cycle[9]]],VertexNumber->On]
In a very similar way we add (isolated) vertices
AddVertices[graph,vert]
As an argument we can use the number of vertices
ShowGraph[g2=AddVertices[g1,4]]
and the specified cordinates of vertices
ShowGraph[g3=AddVertices[g1,{{1,2},{1.5,2.5},{-2,-1}}]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Adding vertices and edges
To add the list list of edges to the existing graph graph we use the
command AddEdges[graph,list]
ShowGraph[g=Star[10],VertexNumber->On]
g1=AddEdges[g,{{1,2},{5,7},{5,7},{9,9}}]
ShowGraph[g1,VertexNumber->On]
ShowGraph[g2=AddEdges[g1,Edges[Cycle[9]]],VertexNumber->On]
In a very similar way we add (isolated) vertices
AddVertices[graph,vert]
As an argument we can use the number of vertices
ShowGraph[g2=AddVertices[g1,4]]
and the specified cordinates of vertices
ShowGraph[g3=AddVertices[g1,{{1,2},{1.5,2.5},{-2,-1}}]]
ShowGraph[g4=AddVertices[g1,1.5*Vertices[g]]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Deleting edges and vertices
You can delete edges by DeleteEdges[graph,list] and
DeleteVertices[graph,list]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Deleting edges and vertices
You can delete edges by DeleteEdges[graph,list] and
DeleteVertices[graph,list]
ShowGraph[g5=DeleteEdges[g4,{{3,10},{5,7}}], VertexNumber
-> On]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Deleting edges and vertices
You can delete edges by DeleteEdges[graph,list] and
DeleteVertices[graph,list]
ShowGraph[g5=DeleteEdges[g4,{{3,10},{5,7}}], VertexNumber
-> On]
and for vertices
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Deleting edges and vertices
You can delete edges by DeleteEdges[graph,list] and
DeleteVertices[graph,list]
ShowGraph[g5=DeleteEdges[g4,{{3,10},{5,7}}], VertexNumber
-> On]
and for vertices
ShowGraph[g6=DeleteVertices[g5,{9}]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Contraction
Contraction of vertices v1 , ..., vk is to replace these vertices by a new
vertex v adjacent to all this vertices which were adjacent to at least one
of v1 , ..., vk before contraction
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Contraction
Contraction of vertices v1 , ..., vk is to replace these vertices by a new
vertex v adjacent to all this vertices which were adjacent to at least one
of v1 , ..., vk before contraction
It is realizing by Contract[graph,list-of-vertices]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Contraction
Contraction of vertices v1 , ..., vk is to replace these vertices by a new
vertex v adjacent to all this vertices which were adjacent to at least one
of v1 , ..., vk before contraction
It is realizing by Contract[graph,list-of-vertices]
ShowGraph[g=CompleteGraph[5],VertexNumber->On]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Contraction
Contraction of vertices v1 , ..., vk is to replace these vertices by a new
vertex v adjacent to all this vertices which were adjacent to at least one
of v1 , ..., vk before contraction
It is realizing by Contract[graph,list-of-vertices]
ShowGraph[g=CompleteGraph[5],VertexNumber->On]
ShowGraph[g1=Contract[g,{1,2,3}],VertexNumber->On]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Podgraf indukowany
Let us remind that a subgraph induced by the subset VH of all vertices
of G is the subgraph of G consisting of all vertices VH and these edges
of G which are adjacent only to vertices from the set VH
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Podgraf indukowany
Let us remind that a subgraph induced by the subset VH of all vertices
of G is the subgraph of G consisting of all vertices VH and these edges
of G which are adjacent only to vertices from the set VH
We have InduceSubgraph[graph,list-of-vertices]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Podgraf indukowany
Let us remind that a subgraph induced by the subset VH of all vertices
of G is the subgraph of G consisting of all vertices VH and these edges
of G which are adjacent only to vertices from the set VH
We have InduceSubgraph[graph,list-of-vertices]
ShowGraphArray[{g,g2=InduceSubgraph[g,{1,2,3,4}]}]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Union and interscetion
To get the union of G and H use the command
GraphUnion[graph1,graph2]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Union and interscetion
To get the union of G and H use the command
GraphUnion[graph1,graph2]
ShowGraph[g3=GraphUnion[CompleteGraph[4],CompleteBinaryTree[4]]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Union and interscetion
To get the union of G and H use the command
GraphUnion[graph1,graph2]
ShowGraph[g3=GraphUnion[CompleteGraph[4],CompleteBinaryTree[4]]]
For the intersection we use the command
GraphIntersection[graph1,...,graphk]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Union and interscetion
To get the union of G and H use the command
GraphUnion[graph1,graph2]
ShowGraph[g3=GraphUnion[CompleteGraph[4],CompleteBinaryTree[4]]]
For the intersection we use the command
GraphIntersection[graph1,...,graphk]
ShowGraphArray[{
g5=SetGraphOptions[Star[9],VertexLabel->{1,2,3,4,5,6,7,8,9}],
g6=SetGraphOptions[GridGraph[3,3],VertexLabel->{1,2,3,4,5,6,7,8,9}],
GraphIntersection[g5,g6]}]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Union and interscetion
To get the union of G and H use the command
GraphUnion[graph1,graph2]
ShowGraph[g3=GraphUnion[CompleteGraph[4],CompleteBinaryTree[4]]]
For the intersection we use the command
GraphIntersection[graph1,...,graphk]
ShowGraphArray[{
g5=SetGraphOptions[Star[9],VertexLabel->{1,2,3,4,5,6,7,8,9}],
g6=SetGraphOptions[GridGraph[3,3],VertexLabel->{1,2,3,4,5,6,7,8,9}],
GraphIntersection[g5,g6]}]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Join
The join of G and H is the graph which is the union of G i H but
additionally with all edges joining all vertices of G and H. To get the
join write GraphJoin[g1,...,gk]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Join
The join of G and H is the graph which is the union of G i H but
additionally with all edges joining all vertices of G and H. To get the
join write GraphJoin[g1,...,gk]
ShowGraphArray[{g7=Cycle[10],
g8=EmptyGraph[1],g9=GraphJoin[g7,g8],SpringEmbedding[g9]}]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Join
The join of G and H is the graph which is the union of G i H but
additionally with all edges joining all vertices of G and H. To get the
join write GraphJoin[g1,...,gk]
ShowGraphArray[{g7=Cycle[10],
g8=EmptyGraph[1],g9=GraphJoin[g7,g8],SpringEmbedding[g9]}]
ShowGraphArray[{g10=Wheel[10],g11=EmptyGraph[1],
g12=GraphJoin[g10,g11]}]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Join
The join of G and H is the graph which is the union of G i H but
additionally with all edges joining all vertices of G and H. To get the
join write GraphJoin[g1,...,gk]
ShowGraphArray[{g7=Cycle[10],
g8=EmptyGraph[1],g9=GraphJoin[g7,g8],SpringEmbedding[g9]}]
ShowGraphArray[{g10=Wheel[10],g11=EmptyGraph[1],
g12=GraphJoin[g10,g11]}]
SpringEmbedding[g12]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Complement
The complement of a graph G is the graph with the same set of vertices
and only these edges joining its vertices which are not edges of G . To
get it we use GraphComplement[graph]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Complement
The complement of a graph G is the graph with the same set of vertices
and only these edges joining its vertices which are not edges of G . To
get it we use GraphComplement[graph]
ShowGraphArray[{g=CompleteGraph[5],g1=CircularEmbedding[Star[5]],
g2=GraphComplement[g1]}]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Connectivity of undirected graphs
Weakly and strongly connected components
The command ConnectedComponents[graph] returns the list of lists of
all vertices with division into the components.
ShowGraph[g3=GraphUnion[CompleteGraph[4],CompleteBinaryTree[4]]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Connectivity of undirected graphs
Weakly and strongly connected components
The command ConnectedComponents[graph] returns the list of lists of
all vertices with division into the components.
ShowGraph[g3=GraphUnion[CompleteGraph[4],CompleteBinaryTree[4]]]
l=ConnectedComponents[g3]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Connectivity of undirected graphs
Weakly and strongly connected components
The command ConnectedComponents[graph] returns the list of lists of
all vertices with division into the components.
ShowGraph[g3=GraphUnion[CompleteGraph[4],CompleteBinaryTree[4]]]
l=ConnectedComponents[g3]
We can find the number of components as the length of the list
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Connectivity of undirected graphs
Weakly and strongly connected components
The command ConnectedComponents[graph] returns the list of lists of
all vertices with division into the components.
ShowGraph[g3=GraphUnion[CompleteGraph[4],CompleteBinaryTree[4]]]
l=ConnectedComponents[g3]
We can find the number of components as the length of the list
Length[l]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Connectivity of undirected graphs
Weakly and strongly connected components
The command ConnectedComponents[graph] returns the list of lists of
all vertices with division into the components.
ShowGraph[g3=GraphUnion[CompleteGraph[4],CompleteBinaryTree[4]]]
l=ConnectedComponents[g3]
We can find the number of components as the length of the list
Length[l]
The complement of a connected graph is disconnected
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Connectivity of undirected graphs
Weakly and strongly connected components
The command ConnectedComponents[graph] returns the list of lists of
all vertices with division into the components.
ShowGraph[g3=GraphUnion[CompleteGraph[4],CompleteBinaryTree[4]]]
l=ConnectedComponents[g3]
We can find the number of components as the length of the list
Length[l]
The complement of a connected graph is disconnected
ShowGraph[g4=GraphComplement[g3]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Connectivity of undirected graphs
Weakly and strongly connected components
The command ConnectedComponents[graph] returns the list of lists of
all vertices with division into the components.
ShowGraph[g3=GraphUnion[CompleteGraph[4],CompleteBinaryTree[4]]]
l=ConnectedComponents[g3]
We can find the number of components as the length of the list
Length[l]
The complement of a connected graph is disconnected
ShowGraph[g4=GraphComplement[g3]]
We can check the above also by the command ConnectedQ[graf]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Connectivity of undirected graphs
Weakly and strongly connected components
The command ConnectedComponents[graph] returns the list of lists of
all vertices with division into the components.
ShowGraph[g3=GraphUnion[CompleteGraph[4],CompleteBinaryTree[4]]]
l=ConnectedComponents[g3]
We can find the number of components as the length of the list
Length[l]
The complement of a connected graph is disconnected
ShowGraph[g4=GraphComplement[g3]]
We can check the above also by the command ConnectedQ[graf]
ConnectedQ[g4]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Connectivity of undirected graphs
Weakly and strongly connected components
The command WeaklyConnectedComponents[graph] returns the list of
lists of vertices with division into the weakly connected components
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Connectivity of undirected graphs
Weakly and strongly connected components
The command WeaklyConnectedComponents[graph] returns the list of
lists of vertices with division into the weakly connected components
Polecenie StronglyConnectedComponents[graph] returns the list of
lists of vertices with division into the strongly connected components
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Connectivity of undirected graphs
Weakly and strongly connected components
The command WeaklyConnectedComponents[graph] returns the list of
lists of vertices with division into the weakly connected components
Polecenie StronglyConnectedComponents[graph] returns the list of
lists of vertices with division into the strongly connected components
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Connectivity of undirected graphs
Weakly and strongly connected components
Exercise
For the given graph:
draw the graph by FromOrderedPairs[list]
find strongly connected componente
draw the component
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
BFS
To see the result of BFS we use:
BreadthFirstTraversal[graph,root,parameter].
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
BFS
To see the result of BFS we use:
BreadthFirstTraversal[graph,root,parameter].
without parameter the command returns the list of vertices of BFS tree
ShowGraph[g=ButterflyGraph[2],VertexNumber->On]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
BFS
To see the result of BFS we use:
BreadthFirstTraversal[graph,root,parameter].
without parameter the command returns the list of vertices of BFS tree
ShowGraph[g=ButterflyGraph[2],VertexNumber->On]
BreadthFirstTraversal[g,1]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
BFS
To see the result of BFS we use:
BreadthFirstTraversal[graph,root,parameter].
without parameter the command returns the list of vertices of BFS tree
ShowGraph[g=ButterflyGraph[2],VertexNumber->On]
BreadthFirstTraversal[g,1]
with parametr=Edge the command returns the list of edges with the
order of visiting
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
BFS
To see the result of BFS we use:
BreadthFirstTraversal[graph,root,parameter].
without parameter the command returns the list of vertices of BFS tree
ShowGraph[g=ButterflyGraph[2],VertexNumber->On]
BreadthFirstTraversal[g,1]
with parametr=Edge the command returns the list of edges with the
order of visiting
ge=BreadthFirstTraversal[g,1,Edge]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
BFS
To see the result of BFS we use:
BreadthFirstTraversal[graph,root,parameter].
without parameter the command returns the list of vertices of BFS tree
ShowGraph[g=ButterflyGraph[2],VertexNumber->On]
BreadthFirstTraversal[g,1]
with parametr=Edge the command returns the list of edges with the
order of visiting
ge=BreadthFirstTraversal[g,1,Edge]
Wreszcie parametr=Tree returns the BFS tree
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
BFS
To see the result of BFS we use:
BreadthFirstTraversal[graph,root,parameter].
without parameter the command returns the list of vertices of BFS tree
ShowGraph[g=ButterflyGraph[2],VertexNumber->On]
BreadthFirstTraversal[g,1]
with parametr=Edge the command returns the list of edges with the
order of visiting
ge=BreadthFirstTraversal[g,1,Edge]
Wreszcie parametr=Tree returns the BFS tree
ShowGraph[g1=BreadthFirstTraversal[g,1,Tree],VertexNumber->On]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
DFS
In an analogous way we use:
DepthFirstTraversal[graf,root,parametr] to illustrate DFS
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
DFS
In an analogous way we use:
DepthFirstTraversal[graf,root,parametr] to illustrate DFS
h=GridGraph[3,3,3]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
DFS
In an analogous way we use:
DepthFirstTraversal[graf,root,parametr] to illustrate DFS
h=GridGraph[3,3,3]
ShowGraphArray[{h,BreadthFirstTraversal[h,1,Tree],
DepthFirstTraversal[h,1,Tree]},VertexStyle->Disk[0.05],
VertexNumber->On]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
DFS
In an analogous way we use:
DepthFirstTraversal[graf,root,parametr] to illustrate DFS
h=GridGraph[3,3,3]
ShowGraphArray[{h,BreadthFirstTraversal[h,1,Tree],
DepthFirstTraversal[h,1,Tree]},VertexStyle->Disk[0.05],
VertexNumber->On]
ShowGraphArray[{RootedEmbedding[h,1],
RootedEmbedding[BreadthFirstTraversal[h,1,Tree],1],
RootedEmbedding[DepthFirstTraversal[h,1,Tree],1]},
VertexStyle->Disk[0.05],VertexNumber->On]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Finding circuits
The command TreeQ[graph] tests acyclicity of a graph:
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Finding circuits
The command TreeQ[graph] tests acyclicity of a graph:
ShowGraph[g=RealizeDegreeSequence[{3,4,3,4,3,5}]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Finding circuits
The command TreeQ[graph] tests acyclicity of a graph:
ShowGraph[g=RealizeDegreeSequence[{3,4,3,4,3,5}]]
TreeQ[g]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Finding circuits
The command TreeQ[graph] tests acyclicity of a graph:
ShowGraph[g=RealizeDegreeSequence[{3,4,3,4,3,5}]]
TreeQ[g]
The command FindCycle[graf] returns the cycle (if exits)
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Finding circuits
The command TreeQ[graph] tests acyclicity of a graph:
ShowGraph[g=RealizeDegreeSequence[{3,4,3,4,3,5}]]
TreeQ[g]
The command FindCycle[graf] returns the cycle (if exits)
c=FindCycle[g]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Finding circuits
The command TreeQ[graph] tests acyclicity of a graph:
ShowGraph[g=RealizeDegreeSequence[{3,4,3,4,3,5}]]
TreeQ[g]
The command FindCycle[graf] returns the cycle (if exits)
c=FindCycle[g]
we can see the list of edges of the cycle using:
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Finding circuits
The command TreeQ[graph] tests acyclicity of a graph:
ShowGraph[g=RealizeDegreeSequence[{3,4,3,4,3,5}]]
TreeQ[g]
The command FindCycle[graf] returns the cycle (if exits)
c=FindCycle[g]
we can see the list of edges of the cycle using:
ShowGraph[Highlight[g,{Partition[c,2,1]}]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Finding circuits
The command TreeQ[graph] tests acyclicity of a graph:
ShowGraph[g=RealizeDegreeSequence[{3,4,3,4,3,5}]]
TreeQ[g]
The command FindCycle[graf] returns the cycle (if exits)
c=FindCycle[g]
we can see the list of edges of the cycle using:
ShowGraph[Highlight[g,{Partition[c,2,1]}]]
The command ExtractCycles[graph] returns the maximal list of
disjoint cycles:
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Finding circuits
The command TreeQ[graph] tests acyclicity of a graph:
ShowGraph[g=RealizeDegreeSequence[{3,4,3,4,3,5}]]
TreeQ[g]
The command FindCycle[graf] returns the cycle (if exits)
c=FindCycle[g]
we can see the list of edges of the cycle using:
ShowGraph[Highlight[g,{Partition[c,2,1]}]]
The command ExtractCycles[graph] returns the maximal list of
disjoint cycles:
ExtractCycles[g]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Euler cycle
To check if a graph possesses an Euler cycle we usEulerianQ[graph]:
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Euler cycle
To check if a graph possesses an Euler cycle we usEulerianQ[graph]:
ShowGraph[g=RealizeDegreeSequence[{4,2,2,2,4}],VertexNumber->On]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Euler cycle
To check if a graph possesses an Euler cycle we usEulerianQ[graph]:
ShowGraph[g=RealizeDegreeSequence[{4,2,2,2,4}],VertexNumber->On]
EulerianQ[g]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Euler cycle
To check if a graph possesses an Euler cycle we usEulerianQ[graph]:
ShowGraph[g=RealizeDegreeSequence[{4,2,2,2,4}],VertexNumber->On]
EulerianQ[g]
The command EulerianCycle[graph] returns an Euler cycle (if exists)
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Euler cycle
To check if a graph possesses an Euler cycle we usEulerianQ[graph]:
ShowGraph[g=RealizeDegreeSequence[{4,2,2,2,4}],VertexNumber->On]
EulerianQ[g]
The command EulerianCycle[graph] returns an Euler cycle (if exists)
c=EulerianCycle[g]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Euler cycle
To check if a graph possesses an Euler cycle we usEulerianQ[graph]:
ShowGraph[g=RealizeDegreeSequence[{4,2,2,2,4}],VertexNumber->On]
EulerianQ[g]
The command EulerianCycle[graph] returns an Euler cycle (if exists)
c=EulerianCycle[g]
ShowGraph[Highlight[g,{Partition[c,2,1]}]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Hamiltonian cycle
The command HamiltonianCycle[graph] returns a Hamiltonian cycle
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Hamiltonian cycle
The command HamiltonianCycle[graph] returns a Hamiltonian cycle
HamiltonianCycle[CompleteGraph[5]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Hamiltonian cycle
The command HamiltonianCycle[graph] returns a Hamiltonian cycle
HamiltonianCycle[CompleteGraph[5]]
To get all cycles use the parameter All:
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Hamiltonian cycle
The command HamiltonianCycle[graph] returns a Hamiltonian cycle
HamiltonianCycle[CompleteGraph[5]]
To get all cycles use the parameter All:
HamiltonianCycle[CompleteGraph[5],All]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Hamiltonian cycle
The command HamiltonianCycle[graph] returns a Hamiltonian cycle
HamiltonianCycle[CompleteGraph[5]]
To get all cycles use the parameter All:
HamiltonianCycle[CompleteGraph[5],All]
Also we can highlight this cycle:
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Hamiltonian cycle
The command HamiltonianCycle[graph] returns a Hamiltonian cycle
HamiltonianCycle[CompleteGraph[5]]
To get all cycles use the parameter All:
HamiltonianCycle[CompleteGraph[5],All]
Also we can highlight this cycle:
ShowGraph[Highlight[g=CompleteGraph[5],
{Partition[HamiltonianCycle[g],2,1]}]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Search
Circuits
Hamiltonian cycle
The command HamiltonianCycle[graph] returns a Hamiltonian cycle
HamiltonianCycle[CompleteGraph[5]]
To get all cycles use the parameter All:
HamiltonianCycle[CompleteGraph[5],All]
Also we can highlight this cycle:
ShowGraph[Highlight[g=CompleteGraph[5],
{Partition[HamiltonianCycle[g],2,1]}]]
ShowGraph[Highlight[g=DodecahedralGraph,
{Partition[HamiltonianCycle[g],2,1]}]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Weighted graphs
We can assign the weights to the edges by
SetEdgesWeight[graph,list]. The function returns the weighted
graph.
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Weighted graphs
We can assign the weights to the edges by
SetEdgesWeight[graph,list]. The function returns the weighted
graph.
g=Wheel[5]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Weighted graphs
We can assign the weights to the edges by
SetEdgesWeight[graph,list]. The function returns the weighted
graph.
g=Wheel[5]
ShowGraph[g,VertexNumber->On]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Weighted graphs
We can assign the weights to the edges by
SetEdgesWeight[graph,list]. The function returns the weighted
graph.
g=Wheel[5]
ShowGraph[g,VertexNumber->On]
Edges[g]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Weighted graphs
We can assign the weights to the edges by
SetEdgesWeight[graph,list]. The function returns the weighted
graph.
g=Wheel[5]
ShowGraph[g,VertexNumber->On]
Edges[g]
h=SetEdgeWeights[g,{1,2,3,1,2,4,2,6}]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Weighted graphs
We can assign the weights to the edges by
SetEdgesWeight[graph,list]. The function returns the weighted
graph.
g=Wheel[5]
ShowGraph[g,VertexNumber->On]
Edges[g]
h=SetEdgeWeights[g,{1,2,3,1,2,4,2,6}]
To show the result we can display graph with labels the same as the
weights. We use here polecenie SetEdgeLabels[graph,list]:
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Weighted graphs
We can assign the weights to the edges by
SetEdgesWeight[graph,list]. The function returns the weighted
graph.
g=Wheel[5]
ShowGraph[g,VertexNumber->On]
Edges[g]
h=SetEdgeWeights[g,{1,2,3,1,2,4,2,6}]
To show the result we can display graph with labels the same as the
weights. We use here polecenie SetEdgeLabels[graph,list]:
ShowGraph[SetEdgeLabels[h,GetEdgeWeights[h]],VertexNumber->On,
TextStyle->{FontSize->12,FontColor->Red}]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
TSP
The sequence of optimal cycle can be got by
TravelingSalesman[graf]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
TSP
The sequence of optimal cycle can be got by
TravelingSalesman[graf]
h=SetEdgeWeights[g,{20,15,30,75,30,20,40,65,25,40}]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
TSP
The sequence of optimal cycle can be got by
TravelingSalesman[graf]
h=SetEdgeWeights[g,{20,15,30,75,30,20,40,65,25,40}]
ShowGraph[SetEdgeLabels[h,GetEdgeWeights[h]],VertexNumber->On,
TextStyle->{FontSize->12,FontColor->Red}]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
TSP
The sequence of optimal cycle can be got by
TravelingSalesman[graf]
h=SetEdgeWeights[g,{20,15,30,75,30,20,40,65,25,40}]
ShowGraph[SetEdgeLabels[h,GetEdgeWeights[h]],VertexNumber->On,
TextStyle->{FontSize->12,FontColor->Red}]
TravelingSalesman[h]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
TSP
The sequence of optimal cycle can be got by
TravelingSalesman[graf]
h=SetEdgeWeights[g,{20,15,30,75,30,20,40,65,25,40}]
ShowGraph[SetEdgeLabels[h,GetEdgeWeights[h]],VertexNumber->On,
TextStyle->{FontSize->12,FontColor->Red}]
TravelingSalesman[h]
ShowGraph[Highlight[h,{Partition[TravelingSalesman[h],2,1]}]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
TSP
The sequence of optimal cycle can be got by
TravelingSalesman[graf]
h=SetEdgeWeights[g,{20,15,30,75,30,20,40,65,25,40}]
ShowGraph[SetEdgeLabels[h,GetEdgeWeights[h]],VertexNumber->On,
TextStyle->{FontSize->12,FontColor->Red}]
TravelingSalesman[h]
ShowGraph[Highlight[h,{Partition[TravelingSalesman[h],2,1]}]]
To find the weight of optimal cycle we use:
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
TSP
The sequence of optimal cycle can be got by
TravelingSalesman[graf]
h=SetEdgeWeights[g,{20,15,30,75,30,20,40,65,25,40}]
ShowGraph[SetEdgeLabels[h,GetEdgeWeights[h]],VertexNumber->On,
TextStyle->{FontSize->12,FontColor->Red}]
TravelingSalesman[h]
ShowGraph[Highlight[h,{Partition[TravelingSalesman[h],2,1]}]]
To find the weight of optimal cycle we use:
CostOfPath[h,TravelingSalesman[h]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Dijkstra algorithm
For a given graph with nonnegative weight function the command
Dijkstra[g,v] returns two lists: the list of ancestors and the list of the
length of shortest paths from v to the remaining vertces.
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Dijkstra algorithm
For a given graph with nonnegative weight function the command
Dijkstra[g,v] returns two lists: the list of ancestors and the list of the
length of shortest paths from v to the remaining vertces.
the result can be displayed also in the table form by
TableForm[Dijkstra[g,v]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Dijkstra algorithm
Exercise
Examine Dijsktra algorithm for the graph:
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Bellman-Ford algorithm
The command BellmanFord[g,v] acts in the similar way as Dijkstra .
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Bellman-Ford algorithm
The command BellmanFord[g,v] acts in the similar way as Dijkstra .
TableForm[BellmanFord[g,v]]
Marek Majewski
Wolfram Mathematica in Graph Theory
Graph implementation
Operations on graphs
Connectivity
Algoirthms
TSP problem
Shortest paths
Bellman-Ford algorithm
Exercise
Examine the Bellman-Ford algorithm for the graph:
Marek Majewski
Wolfram Mathematica in Graph Theory