Академический Документы
Профессиональный Документы
Культура Документы
Undirected Graph
- The AddEdge command adds one or more edges to an undirected graph. By
default, the original graph is changed to a graph containing the specified set of
edge(s). Theo default, th ban u s thay i thnh th mi cha tp cc cnh
thm vo. By setting inplace = false the original graph remains unchanged. Bng thit
lp inplace = false, th ban u s ko thay i.
- If the graph is weighted, then a weighted edge can be added by calling AddEdge
with one or more edges in the form [edge, weight], where the edge is just the set of
two vertices (nh), and the weight represents (i din) the value of the edge weight.
> with( GraphTheory):
G := CycleGraph(5):
AddEdge( G, {{1, 3}, {2, 4}}, inplace = true);
DrawGraph(G);
Graph G: an undirected unweighted graph with 5 vertices and 7 edge(s)
AdjacencyMatrix(G);
0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0
Directed Graph
1/ > with( GraphTheory):
G := Digraph( [a, b, c, d, e], {[a, b], [b, c], [c, d], [d, e]} );
4
DrawGraph(G);
0
0
0
1
1
0
0
0
0
1
0
0
0
0
1
0
1
2
3
4
5
6
1
0
1
0
4
0
0
2
1
0
1
0
4
0
3
0
1
0
3
0
1
4
4
0
3
0
1
0
5
0
4
0
1
0
4
6
0
0
1
0
4
0
T := MinimalSpanningTree(G);
DrawGraph(T);
1 2; 2 3; 3 4; 3 6; 4 5;
Edges(T, weights);
{ [{1, 2}, 1], [{2, 3}, 1], [{3, 4}, 3], [{3, 6}, 1], [{4, 5}, 1] }
PrimsAlgorithm( G, 'w', animate);
w;
WeightMatrix(G)
> G := Graph( { [{1, 2}, 1], [{2, 3}, 3], [{3, 4}, 7], [{2, 4}, 2], [{4, 1}, 15] } );
DijkstrasAlgorithm( G, 1, 4);
DrawGraph(G);
[ [1, 2, 4], 3 ]
> WeightMatrix(G)
0
1
0
1
0
3
0
3
0
9
15
2
7
15
10
> DegreeSequence(G);
with(networks)
V th K5.
> G := CycleGraph(5);
DrawGraph(G);
AddEdge( G, { {1, 3}, {1, 4}, {2, 4}, {2, 5}, {3, 1}, {3, 5}, {4, 1}, {4, 5} } );
DrawGraph(G);
> with(GraphTheory):
G := Graph( [1, 2, 3, 4, 5]);
for i from 1 to 5 do
for j from 1 to 5 do
if (i != j) then
AddEdge( G, {i, j});
end if;
end do;
11
end do;
DrawGraph(G);
th K5.
AdjacencyMatrix(G);
1
2
3
4
5
1
0
1
1
1
1
2
1
0
1
1
1
3
1
1
0
1
1
4
1
1
1
0
1
5
1
1
1
1
0
DijkstrasAlgorithm( G, 1 );
V th K6
G := Graph( [1, 2, 3, 4, 5, 6]);
DrawGraph(G);
for i from 1 to 6 do
for j from 1 to 6 do
if (i != j) then
AddEdge( G, {i, j});
end if;
end do;
end do;
DrawGraph(G);
12
AdjacencyMatrix(G);
1
2
3
4
1
0
1
1
0
2
1
0
1
1
3
1
1
0
1
4
0
1
1
0
5
1
0
1
1
6
1
1
0
1
13
5
6
1
1
0
1
1
0
1
1
0
1
1
0
V th K(3, 4)
G := Graph( [1, 2, 3, 4, 5, 6, 7]);
for i from 1 to 3 do
for j from 4 to 7 do
if (i != j) then
AddEdge( G, {i, j});
end if;
end do;
end do;
DrawGraph(G);
V th W6
G := Graph( [1, 2, 3, 4, 5, 6], Trail(1, 2, 3, 4, 5, 1) );
for i from 1 to 5 do
AddEdge( G, {i, 6});
end do;
14
DrawGraph(G);
DijkstrasAlgorithm( G, 1 );
> with(GraphTheory):
with(SpecialGraphs):
G := GeneralizedPetersenGraph(5, 2);
DrawGraph(G);
> with(GraphTheory):
with(SpecialGraphs):
G := GeneralizedPetersenGraph(6, 2);
DrawGraph(G);
15
> with(GraphTheory):
with(SpecialGraphs):
G := GeneralizedPetersenGraph(7, 2);
DrawGraph(G);
> with(GraphTheory):
with(SpecialGraphs):
G := WheelGraph(6);
DrawGraph(G);
16
Prims Algorithm
Tm cy khung c trng s nh nht.
> with(GraphTheory):
G := Graph( { [{1, 2}, 1], [{1, 3}, 1], [{1, 4}, 1], [{1, 5}, 1], [{2, 3}, 1], [{2, 4}, 1],
[{2, 5}, 1], [{3, 4}, 1], [{3, 5}, 1], [{4, 5}, 1] } );
DrawGraph(G);
PrimsAlgorithm(G);
> with(GraphTheory):
17
G := Graph( { [{1, 2}, 3], [{1, 3}, 24], [{1, 4}, 35], [{1, 5}, 16], [{2, 3}, 5], [{2, 4},
6], [{2, 5}, 17], [{3, 4}, 7], [{3, 5}, 38], [{4, 5}, 19] } );
DrawGraph(G);
1
0
3
24
35
2
3
0
5
6
3
24
5
0
7
4
35
6
7
0
5
16
17
38
19
18
16
17
38
19
Gii thut:
- Step 1: Chn ty nh x 0 X v khi to V := { x0 } , T : = , V l tp nh, T
l tp cnh.
- Step 2: Trong s nhng cnh ni nh x0 vi y m x 0 V and y X \ V , ta chn
cnh e c trng s nh nht. Nu ko c cnh e: Dng (1).
- Step 3: Gn V := V {y}, and T := T {e}.
- Step 4: Nu T n-1 phn t th Dng (2), ngc li lm tip bc 2.
Ghi ch: Khi thut ton dng theo trng hp 1 (Tm ko c cnh t x i ra) th th
ko lin thng nn ko c cy khung.
> with(GraphTheory):
G := Graph( { [{1, 2}, 3], [{1, 3}, 24], [{1, 4}, 35], [{1, 5}, 16], [{2, 3}, 5], [{2, 4},
6], [{2, 5}, 17], [{3, 4}, 7], [{3, 5}, 38], [{4, 5}, 19] } );
DrawGraph(G);
Prim Algorithm
- Step 1: chn nh 1, trong s nhng cnh k vi nh 1, chn cnh {1, 2} c
trng s min l 3.
- Step 2: chn nh 2, trong s nhng cnh k vi nh 1, 2, chn cnh {2, 3} c
trng s min l 5.
- Step 3: trong s nhng cnh k vi nh 1, 2, 3 chn cnh {2, 4} c trng s min
l 6.
- Step 4: trong s nhng cnh k vi nh 1, 2, 3, 4 chn cnh {3, 4} c trng s
min l 7. {2, 3, 4} to thnh chu trnh nn b cnh {3, 4}.
- Step 5: trong s nhng cnh k vi nh 1, 2, 3, 4, chn cnh {1, 5} c trng s
min l 16.
Kruskal Algorithm
- Step 1: chn cnh {1, 2} c trng s min l 3.
- Step 2: chn cnh {2, 3} c trng s min l 5.
22
Step 2: Duyt theo cnh e thuc danh sch sp xp. Nu T + e ko cha chu trnh
th:
Ghp cy e vo cy: T = T + {e};
Nu T n 1 phn t th dng.
#include <iostream.h>
#include <stdio.h>
#define MAX 100
class Kruskal {
private:
struct Edge1 {
int u, v, weight;
} Edge[MAX];
int Tree[MAX][2], set[MAX]; int n;
public:
int ReadEdges();
void MakeSet();
int Find (int vertex);
void Join (int v1, int v2);
void Arrange (int k);
int SpanningTree (int k);
void Display (int k);
};
int Kruskal :: ReadEdges() {
int i, j, k, m; k = 1; n = 5; m = 0;
int x[] = { 3, 24, 35, 16, 5, 6, 17, 7, 38, 19 };
for (i = 1; i <= n; i++)
for (j = i+1; j <= n; j++) {
Edge[k].u = i; Edge[k].v = j;
Edge[k++].weight = x[m]; m++;
}
return (k - 1);
}
void Kruskal :: MakeSet() {
int i;
for (i = 1; i <= n; i++)
23
set[i] = i;
}
int Kruskal :: Find (int vertex) {
return (set[vertex]);
}
void Kruskal :: Join (int v1, int v2) {
int i, j;
if (v1 < v2) {
printf ("if (v1 = %d < v2 = %d) \n", v1, v2);
printf ("set [v2] = v1; set[%d] = %d \n", v2, v1);
set[v2] = v1; }
else {
printf ("if (v1 = %d > v2 = %d) \n", v1, v2);
printf ("set[v1] = v2; set[%d] = %d \n", v1, v2);
set[v1] = v2; }
}
void Kruskal :: Arrange (int k) {
int i, j;
struct Edge1 temp;
for (i = 1; i < k; i++)
for (j = 1; j <= k - i; j++)
if (Edge[j].weight > Edge[j + 1].weight) {
temp = Edge[j];
Edge[j] = Edge[j + 1];
Edge[j + 1] = temp;
}
}
int Kruskal :: SpanningTree (int k) {
int i, t, j, sum, k1, k2; Arrange(k); t = 1; sum = 0;
for (i = 1; i <= k; i++)
printf ("Edge (%d, %d): %d \n", Edge[i].u, Edge[i].v, Edge[i].weight);
for (i = 1; i <= n; i++)
if (Find (Edge[i].u) != Find (Edge[i].v)) {
for (j = 1; j <= n; j++)
printf ("set[%d] = %d ", j, set[j]);
k1 = Edge[i].u; k2 = Edge[i].v;
printf ("\n if (Find (Edge[%d].u) != Find (Edge[%d].v)) \n", i, i);
printf (" Find(%d) = set [%d] = %d \n", k1, k1, set[k1]);
printf (" Find(%d) = set [%d] = %d \n", k2, k2, set[k2]);
Tree[t][1] = Edge[i].u;
Tree[t][2] = Edge[i].v;
printf ("Tree[%d][1] = Edge[%d].u = %d \n", t, i, Edge[i].u);
printf ("Tree[%d][1] = Edge[%d].v = %d \n", t, i, Edge[i].v);
sum += Edge[i].weight;
printf ("Join (Edge[%d].u, Edge[%d].v); t++ = %d; \n\n", t, t, t+1);
24
int x1[] = { 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0};
for (i = 1; i <= n; i++)
for (j = i+1; j <= n; j++) {
A[j][i] = A[i][j] = x1[k1]; k1++;
}
for (i = 1; i <= n; i++) {
j = i; A[i][j] = 0;
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
cout.width(5);
cout << A[i][j];
} printf ("\n"); }
}
int main() {
int i, v1, v2; Create();
for (v1 = 1; v1 <= n; v1++)
v[v1] = 0;
DFS (1);
for (i = 1; i < 2*n-1; i += 2)
printf ("%d - %d \n", T[i], T[i+1]);
return 0;
}
Code Maple DFS
with(GraphTheory):
G := Graph( [1, 2, 3, 4, 5, 6, 7, 8, 9]);
AddEdge( G, { {1, 2}, {1, 3}, {2, 4}, {2, 5}, {2, 6}, {3, 7}, {3, 8}, {3, 9} });
DrawGraph(G);
DFS := proc(k::integer)
local i; global Kt, A, n, E;
Kt := Kt union {k};
for i from 1 to n do
if A(k, i) != 0 and i Kt then
E := AddEdge(E, {k, i} ); DFS(i);
end if;
end do;
end proc:
main := proc(G::Graph, k)
global n, A, E, Kt;
30
n := NumberOfVertices(G); A := AdjacencyMatrix(G);
E := Graph(n); Kt := { };
if not IsConnected(G) then
return ("Do thi nay ko lien thong");
end if;
DFS(k);
return E;
end proc:
with(GraphTheory):
with(RandomGraphs):
G := RandomGraph( 8, 10, connected, weights = 1..10);
T := main(G, 2);
HighlightEdges(G, T, red);
DrawGraph(G);
To 1 mng c 10 phn t = 0 trong Maple:
V := [ seq( 0, i = 1..10) ];
V := array(1..n)
for i from 1 to n do
V[i] := 0;
end do;
31
1
2
3
4
5
6
7
8
9
1
0
2
1
0
3
1
0
0
4
0
1
0
0
5
0
1
0
0
0
6
0
1
0
0
0
0
7
0
0
1
0
0
0
0
8
0
0
1
0
0
0
0
0
9
0
0
1
0
0
0
0
0
0
32
HighlightEdges(G, T, red);
DrawGraph(G);
BFS Code C++
#include <iostream.h>
class Queue {
private:
int data;
Queue *Next;
public:
void Enque (int data);
int Deque();
} *Head, *Tail;
void Queue :: Enque (int data) {
Queue *temp; temp = new Queue;
temp->data = data; temp->Next=NULL;
if (Head == NULL)
Head = temp;
else
Tail->Next = temp;
Tail = temp;
}
int Queue :: Deque() {
Queue* temp; temp = Head;
Head = Head->Next;
return temp->data;
}
int visit[100];
int bfs_span_tree[100][100];
class Graph {
private:
int a;
Graph *Next;
public:
void BFS();
Graph* Create (Graph* Head, int *x, int n);
void Ftraverse (Graph *Head);
};
Graph* Graph :: Create (Graph *Head, int *x, int n) {
int a, i, k; k = 0; Graph *last; Head = last = NULL;
Graph *temp;
for (i = 0; i < n; i++) {
temp = new Graph; temp->a = x[i];
temp->Next = NULL;
35
if (Head == NULL)
Head = temp;
else
last->Next = temp;
last = temp;
}
return Head;
}
void Graph :: Ftraverse (Graph *h) {
while (h != NULL) {
cout << h->a << "->";
h = h->Next; }
cout << "NULL \n";
}
void Graph :: BFS() {
cout << "This program is to implement BFS for an unweighted graph \n";
Graph *ptr[100]; int n, i; n = 2;
int x1[9] = { 1, 2, 4, 6, 7, 3, 5, 1, 6 };
int x2[9] = { 4, 7, 3, 5, 1, 2, 5, 7, 4};
ptr[1] = Create (ptr[1], x1, 9); ptr[2] = Create (ptr[2], x2, 9);
cout << "\n The Entered Graph is ::\n";
for (i = 1; i <= n; i++) {
cout << "< " << i << " > ::";
Ftraverse (ptr[i]);
}
int x;
cout << "\n Enter the start node " << n << ">:"; cin >> x;
cout << "\n The Breadth first search traversal is: \n";
Queue object;
// Mark all the nodes as not viseted
for (i = 1; i <= n; i++)
visit[i] = 0;
for (i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
bfs_span_tree[i][j] = 0;
// Enque the start node
object.Enque (x);
int p;
while (Head != NULL && Tail != NULL) {
// Deque a node
p = object.Deque();
int x = p;
// If p is not visited yet.
while (ptr[p] != NULL) {
if (visit [ptr [p]->a] != 1) {
cout << "node " << ptr[p]->a << " is visited \n";
36
Dijkstra Algorithm
- Cho th G = (X, E) vi cc cnh c trng s ko m.
- D liu nhp cho thut ton l ma trn trng s L v 2 nh x, y cho trc. (vi
quy c L(x, y) = 999 nu ko c cnh ni t nh x n nh y).
- D liu xut l ng i ngn nht t x n y.
Step 1: Assign (gn) T = X, the set of vertices (cc nh) of graph.
Initial (khi ng): length[x] = 0; with every k in X \{x}, length[k] = 999.
last[k] = 0; mark[k] = 0.
- length[k] is the length of path from vertex x to vertex k. last[] is the array to save
the path element. mark[] is the array to mark the elements whether is visited or not.
Step 2: If y not in T, Stop.
Choose vertex v in T so that length[v] is min and remove v out of T. mark[v] = 1.
Step 3:
With every vertex k in T, and there exist an edge from v to k.
If length[k] > length[v] + L(v, k) // weight of edge from v to k.
length[k] = length[v] + L(v, k)
last[k] = v.
Continuos step 2.
Code Dijkstra Algorithm
37
#include <iostream.h>
#include <stdio.h>
class Dijkstra {
private:
int graph[15][15];
int last[15], mark[15], length[15];
int source;
int n; // num_of_vertices
public:
int Minimum();
int Read();
void Initialize (int source);
void Printpath (int i);
void Algorithm (int source);
void Output();
};
int Dijkstra::Read() {
n = 5; int i, j, k = 0;
int x1[] = { 3, 24, 35, 16, 5, 6, 17, 7, 38, 19};
cout << "enter the adjacent matrix: \n";
for (i = 1; i <= n; i++)
for (j = i+1; j <= n; j++) {
graph[j][i] = graph[i][j] = x1[k]; k++;
}
for (i = 1; i <= n; i++)
graph[i][i] = 0;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++)
printf ("%d ", graph[i][j]);
printf ("\n");
}
cout << "\n enter the source vertex \n";
cin >> source;
return source;
}
void Dijkstra::Initialize (int source) {
int i;
for (i = 1; i <= n; i++) {
mark[i] = 0;
length[i] = 999;
last[i] = 0;
}
length [source] = 0;
}
void Dijkstra::Algorithm (int source) {
38
40
41
WeightMatrix(G);
1
2
3
4
5
1
0
3
24
35
16
2
3
0
5
6
17
3
24
5
0
7
38
4
35
6
7
0
19
5
16
17
38
19
0
int x1[] = { 0, 3, 24, 35, 16, 3, 0, 5, 6, 17, 24, 5, 0, 7, 38, 35, 6, 7, 0, 19, 16, 17, 38,
19, 0 };
int x1[] = { 3, 24, 35, 16, 5, 6, 17, 7, 38, 19};
DijkstrasAlgorithm( G, 1, 4 );
[ [1, 2, 3, 4], 15]
Tm ng i ngn nht t nh 1 n nh 4;
Step 1: Assign T = X\{x}, the set of vertices of graph.
Initial (khi ng): length[1] = 0; with every k in X \{x}, length[k] = 999.
last[k] = 0; mark[k] = 0.
- length[k] is the length of path from vertex x to vertex k. last[] is the array to save
the path element. mark[] is the array to mark the elements whether is visited or not.
Step 2: If y not in T, Stop.
Choose vertex v in T so that length[v] is min and remove v out of T. mark[v] = 1.
Step 3:
With every vertex k in T, and there exist an edge from v to k.
If length[k] > length[v] + L(v, k) // weight of edge from v to k.
length[k] = length[v] + L(v, k)
last[k] = v.
Continue step 2.
42
1 2 4.
Bellman-Ford Algorithm
- Step 1: Initialize: P[0][x] = 0; P[0][i] = 9999, with every i x, k = 1.
- Step 2: With every vertex i in X, we calculate:
P[k][i] = min ( { P[k-1][i] }, { P[k-1][j] + L[i][j] ). There exists path from i to j.
L[i][j] is the weight matrix.
- Step 3: If row k and row k-1 of matrix P are equal then P[k][i] is the length of
shortest path from x to i.
If row k and row k-1 of matrix P are different
If k < n then increasing k = k + 1 and returning Step 2.
If k == n then stopping cause graph have negative path.
#include
#include
#include
#define
<iostream.h>
<stdio.h>
<stdlib.h>
MAX 20
class bell_ford {
private:
int n; int graph[MAX][MAX]; int start;
int distance[MAX]; int pre[MAX];
public:
void read_graph();
void initialize();
void update();
void check();
void algorithm();
};
void bell_ford::read_graph() {
int i, j, k; n = 5; k = 0;
int x1[] = { 3, 24, 35, 16, 5, 6, 17, 7, 38, 19 };
for (i = 1; i <= n; i++)
for (j = i+1; j <= n; j++) {
graph[j][i] = graph[i][j] = x1[k]; k++; }
for (i = 1; i <= n; i++)
graph[i][i] = 0;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
printf ("%d ", graph[i][j]); }
printf ("\n"); }
cout << "Enter the start vertex ::";
45
return;
} } }
cout << "\n\n There is no negative weight cycle and \n";
cout << "The final paths and the distances are \n";
for (i = 1; i <= n; i++) {
cout << "path for node " << i << " is ::\n";
int arr[MAX], k = 1; j = i;
while (pre[j] != 0) {
printf ("while pre[%d] != 0 \n", j);
arr[k] = pre[j]; k++;
printf ("arr[%d] = pre[%d] = %d \n", k-1, j, arr[k-1]);
printf ("j = pre[%d] = %d, k = %d \n", j, pre[j], k);
j = pre[j];
}
for (k = k - 1; k > 0; k--)
printf ("arr[%d] = %d -> ", k, arr[k]);
cout << i << endl;
cout << "distance is " << distance[i] << endl << endl;
}
}
void bell_ford::algorithm() {
read_graph(); initialize();
update(); check();
}
int main() {
bell_ford obj;
obj.algorithm();
}
BellmanFord Code 2.
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
struct Edge{
int source;
int dest;
int weight;
};
void BellmanFord (Edge edges[], int edgecount, int nodecount, int source) {
int *d = new int [nodecount + 1];
int i, j, k1, k2, k3, d1;
for (i = 0; i <= nodecount; i++)
d[i] = 9999;
47
d[source] = 0;
for (i = 0; i < edgecount; i++) {
k1 = edges[i].source; k2 = d[edges[i].source];
k3 = edges[i].dest;
if (d[edges[i].source] != 9999) {
printf ("\n if (d[%d] = %d != 9999) \n", k1, k2);
d1 = d[edges[i].source] + edges[i].weight;
printf ("d1 = d[%d] + edges[%d].weight = %d \n", k1, i, d1);
printf ("edges[%d].source = %d \n", i, k1);
printf ("edges[%d].dest = %d \n", i, k3);
printf ("edges[%d].weight = %d \n", i, edges[i].weight);
if (d1 < d[edges[i].dest]) {
printf ("if (d1 = %d < d[%d] = %d) \n", d1, k3, d[k3]);
d[edges[i].dest] = d1;
printf ("d[%d] = %d \n", k3, d1);
}
else {
printf ("if (d1 = %d > d[%d] = %d) \n", d1, k3, d[k3]);
printf ("continue \n");
}
}
}
for (i = 0; i < edgecount; i++)
if (d[edges[i].dest] > d[edges[i].source] + edges[i].weight) {
printf ("Negative edge weight cycles detected!");
free(d);
return;
}
for (i = 1; i <= nodecount; i++) {
printf ("The shortest distance between nodes %d and %d is %d \n",
source, i, d[i]);
}
free(d);
return;
}
int main() {
Edge edges[10] = { {1, 2, 3}, {1, 3, 24}, {1, 4, 35}, {1, 5, 16},
{2, 3, 5}, {2, 4, 6}, {2, 5, 17}, {3, 4, 7}, {3, 5, 38}, {4, 5, 19} };
BellmanFord (edges, 10, 5, 1);
return 0;
}
Bellman-Ford run by hand
> with(GraphTheory):
48
G := Graph( { [{1, 2}, 3], [{1, 3}, 24], [{1, 4}, 35], [{1, 5}, 16], [{2, 3}, 5], [{2, 4},
6], [{2, 5}, 17], [{3, 4}, 7], [{3, 5}, 38], [{4, 5}, 19] } );
DrawGraph(G);
BellmanFordAlgorithm( G, 1, 4);
Edge edges[10] = { {1, 2, 3}, {1, 3, 24}, {1, 4, 35}, {1, 5, 16}, {2, 3, 5},
{2, 4, 6}, {2, 5, 17}, {3, 4, 7}, {3, 5, 38}, {4, 5, 19} };
- Step 1: Initialize distance[i] = 9999 with every i 1.
distance [1] = 0; pre[i] = 0 with every i in X (set of vertices).
- Step 2:
for (u = 1; u <= n; u++)
for (v = 1; v <= n; v++)
if (graph[1][2] == 3 != 0)
if (distance[2] > distance[1] + graph[1][2])
distance[2] = 9999; distance[1] = 0; graph[1][2] = 3;
distance[2] = distance[1] + graph[1][2] = 3.
pre[2] = 1;
if (graph[1][3] == 24 != 0)
if (distance[3] > distance[1] + graph[1][3])
distance[3] = 9999; distance[1] = 0; graph[1][3] = 24;
distance[3] = distance[1] + graph[1][3] = 24.
pre[3] = 1;
if (graph[1][4] == 35 != 0)
if (distance[4] > distance[1] + graph[1][4])
distance[4] = 9999; distance[1] = 0; graph[1][4] = 35;
distance[4] = distance[1] + graph[1][4] = 35.
pre[4] = 1;
49
if (graph[1][5] == 16 != 0)
if (distance[5] > distance[1] + graph[1][5])
distance[5] = 9999; distance[1] = 0; graph[1][5] = 16;
distance[5] = distance[1] + graph[1][5] = 16.
pre[5] = 1;
if (graph[2][1] == 3 != 0)
if (distance[1] < distance[2] + graph[2][1])
distance[1] = 0; distance[2] = 3; graph[2][1] = 3;
continue;
if (graph[2][3] == 5 != 0)
if (distance[3] > distance[2] + graph[2][3])
distance[3] = 24; distance[2] = 3; graph[2][3] = 5;
distance[3] = distance[2] + graph[2][3] = 8.
pre[3] = 2;
if (graph[2][4] == 6 != 0)
if (distance[4] > distance[2] + graph[2][4])
distance[4] = 35; distance[2] = 3; graph[2][4] = 6;
distance[4] = distance[2] + graph[2][4] = 9.
pre[4] = 2;
if (graph[2][5] == 17 != 0)
if (distance[5] < distance[2] + graph[2][5])
distance[5] = 16; distance[2] = 3; graph[2][5] = 17;
continue;
path for node 3 is:
while (pre[j] != 0)
arr[1] = pre[j] = pre[3] = 2;
j = pre[3] = 2;
arr[1] = pre[2] = 1;
1 2 3; distance = 8.
Floyd Code
#include <stdio.h>
#include <stdlib.h>
#include <string>
/* Floyd's All pairs shortest path algorithm (O (n^3) ).
input is adjacency matrix output is matrix of shortest paths.
C is the adjacency matrix.
n is the order of the square matrix.
50
} printf("\n");
delete []A; delete []C;
}
int main() {
FloydTest();
}
Code Maple Floyd 1
with(GraphTheory):
G := Graph( { [{1, 2}, 3], [{1, 3}, 24], [{1, 4}, 35], [{1, 5}, 16], [{2, 3}, 5], [{2, 4},
6], [{2, 5}, 17], [{3, 4}, 7], [{3, 5}, 38], [{4, 5}, 19] } );
DrawGraph(G);
Floyd := proc(G::Graph)
local i, j, k, n, A, B;
A := WeightMatrix(G); n := NumberOfVertices(G);
B := Matrix(n);
if not IsConnected(G) then
return ("Do thi nay ko lien thong");
end if;
for i from 1 to n do
for j from 1 to n do
if A[i, j] > 0 then
for k from 1 to n do
if A[i, k] > 0 then
if A[j, k] < A[j, i] + A[i, k] then
B[j, k] := A[j, i] + A[i, k]; end if;
end if;
end do;
end if;
end do; end do;
return B;
end proc:
A := Floyd(G);
Code Maple Floyd 2
with(GraphTheory): with(RandomGraphs):
Floyd := proc(G::Graph)
local M, M1, M2, i, j, a, n, k;
M := WeightMatrix(G); n := NumberOfVertices(G);
k := 1; M1 := Matrix( M, datatype = anything); M2 := Matrix(n);
for i from 1 to n do
for j from 1 to n do
52
53
IsEulerian( G, 'T');
true;
T = Trail( 1, 2, 3, 1, 4, 2, 5, 3, 4, 5, 1);
- Starting from vertex 1; Trong s cc nh k vi nh 1 th nh 2 l nh nh
nht v cnh {1, 2} cha qua, thm cnh {1, 2} vo T.
- Trong s cc nh k vi nh 2 th nh 3 l nh nh nht v cnh {2, 3} cha
qua, thm cnh {2, 3} vo T.
- Trong s cc nh k vi nh 3 th nh 1 l nh nh nht v cnh {3, 1} cha
qua, thm cnh {3, 1} vo T.
- Trong s cc nh k vi nh 1 th nh 4 l nh nh nht v cnh {1, 4} cha
qua, thm cnh {1, 4} vo T.
- Trong s cc nh k vi nh 4 th nh 2 l nh nh nht v cnh {4, 2} cha
qua, thm cnh {4, 2} vo T.
DeleteEdge(G, { {1, 2}, {2, 3}, {3, 1}, {1, 4}, {4, 2} } );
DrawGraph(G);
54
Stack := Matrix([x0]); X := { };
Road := Matrix([ ]);
while Equal( Stack, Matrix([ ]) ) = false do
k := ArrayNumElems(Stack);
x := Stack( 1, k );
i := 1;
while (i < n + 1) and (M(x, i) = 0 or (M(x, i) = 1 and i X) ) do
i := i + 1;
if (i = n + 1) then break; end if;
end do;
if (i < n + 1) then
Stack := <Stack|i>;
M[i, x] := 0;
else if Equal( M, AdjacencyMatrix(G) ) then
M[ Stack( k ), Stack( k - 1 ) ] := 1;
end if;
Road := <Stack( 1, k ) | Road>;
X := X union { Stack( k ) };
Stack := DeleteColumn( Stack, [k] );
end if;
end do;
print(Road);
end proc:
G1 := RandomGraph(5, 8, Connected); DrawGraph(G1);
Euler(G1, 4);
Stack:
with(LinearAlgebra):
Stack := Matrix([3]);
Stack := <Stack | 5>; # Thm phn t 5 vo Stack;
Stack := <Stack | 7>;
k := ArrayNumElems(Stack): # k l s phn t ca Stack;
i := Stack(1, k): # i l phn t cui ca Stack;
j := Stack(1, 1): # i l phn t u ca Stack;
print(`k = `, k, `i = `, i, `j = `, j);
Stack := DeleteColumn( Stack, [k] ); # Delete phn t cui ca Stack;
s := stack[new](1, 2, 3): # Khi to Stack.
stack[push]( 7, s ): # Thm phn t 7 vo Stack;
print(s);
k := stack[depth](s); # k l s phn t ca Stack;
56
IsEulerian(G, T);
T
Trail( 1, 2, 3,/ 1, 4, 2, 5,/ 1, 6, 2, 7,/ 3, 4, 5, 3,/ 6, 4, 7, 5,/ 6, 7, 1)
1
2
3
4
5
6
7
1
0
1
1
1
1
1
1
2
0
1
1
1
1
1
3
0
1
1
1
1
4
0
1
1
1
5
0
1
1
6
0
1
7
0
int x[] = { 1, 1, 1, 1, 1, 1, / 1, 1, 1, 1, 1, / 1, 1, 1, 1, / 1, 1, 1, 1, 1, 1 };
int x[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
Euler Cycle C++ Code
#include <stdio.h>
#include <stack.h>
int Matrix[100][100];
int n;
void Create() {
int i, j, k1; n = 7; k1 = 0;
int x[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
for (i = 0; i < n; i++)
for (j = i+1; j < n; j++) {
57
IsEulerian(G, T);
T
Trail(1, 2, 3, 1, 4, 2, 5, 3, 4, 5, 1);
1
2
3
4
5
1
0
1
1
1
1
2
0
1
1
1
3
0
1
1
4
0
1
5
0
int x[] = { 1, 1, 1, 1, / 1, 1, 1, / 1, 1, 1};
int x[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
Chu trnh Hamilton
Quy tc xc nh th Hamilton
59
Kt := Kt union {i};
if check = false then Road[k+1] := i; end if;
Browse(i, k+1); Kt := Kt \ {i};
end if;
end do;
end if;
end proc:
G1 := PetersenGraph(); AddEdge(G1, {1, 3});
Hamilton(G1);
> with(GraphTheory):
with(SpecialGraphs):
G := PetersenGraph();
AddEdge(G, {1, 3});
DrawGraph(G);
IsHamiltonian(G, 'T');
T = { 1, 2, 9, 8, 5, 4, 10, 6, 7, 3, 1 }
n
8
61
10
h
1
2
3
4
5
6
7
8
9
0
1
0
1
1
0
0
0
1
0
1
0
0
1
0
1
0
0
0
0
0
0
0
1
0
0
1
0
0
0
0
0
1
0
1
0
0
1
0
0
0
0
0
1
0
0
0
0
1
0
1
0
0
1
0
int x[] = { 1, 1, 0, 1, 1, 0, 0, 0, 0, / 1, 0, 0, 0, 0, 0, 1, 0, / 1, 0, 0, 1, 0, 0, 0, / 1, 0, 0, 0,
0, 1, / 0, 0, 1, 0, 0, / 1, 0, 0, 1, / 1, 0, 0, 1, 0, 1 };
int x[] = { 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1,
0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1 };
Code Hamilton C++
#include <stdio.h>
#define max 20
int a[max][max];
int Mark[max], c[max];
int n;
void Create() {
int i, j, k1; n = 10; k1 = 0;
int x[] = { 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0,
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1 };
for (i = 0; i < n; i++)
for (j = i+1; j < n; j++) {
a[j][i] = a[i][j] = x[k1]; k1++; }
for (i = 0; i < n; i++)
a[i][i] = 0;
for (i = 0; i < n; i++) Mark[i] = 0;
c[0] = 0; Mark[0] = 1; // Xuat phat tu dinh 0.
}
void ShowPath() {
for (int k = 0; k < n; k++)
printf ("%4d", c[k]+1); // In ra s hiu nh + 1
printf ("%4d \n", c[0]+1);
}
void Hamilton(int k) {
for (int i = 0; i < n; i++)
if (Mark[i] == 0 && a [ c[k-1] ] [i] != 0) {
Mark[i] = 1; c[k] = i;
62
if (k == n-1) {
if (a [ c[k]] [c[0] ] != 0) ShowPath();
}
else Hamilton(k+1);
Mark[i] = 0;
}
}
int main() {
Create();
Hamilton(1);
}
void Hamilton(int k) {
for (int i = 0; i < n; i++)
if (Mark[i] == 0 && a [ c[k-1] ] [i]) { // Nu nh k cha duyt n v c ni
vi nh va c ghi nhn trc (th j-1, s hiu l c[j-1])
Mark[i] = 1; c[k] = i; // Xem nh duyt nh k v n l nh th j theo trnh
t ghi nhn.
if (k == n-1) { // Nu ghi nhn s nh
if (a [c[k]] [c[0]] != 0) ShowPath(); // Nu nh cui c ni vi nh u
tin th hin th chu trnh.
}
else Hamilton(k+1); // Nu cha ghi nhn n nh th quy tm nh
th j+1.
Mark[i] = 0; // Quay lui chnh l ch ny y.
}
}
IsHamiltonian(G, 'T');
T = { 1, 2, 9, 8, 5, 4, 10, 6, 7, 3, 1 }
63
- Bt u t nh 1, ly cnh {1, 2}, {1, 3}, xa cnh {1, 6}, {1, 5} (do ly 2
cnh k vi nh 1). Ly {2, 3} s to thnh chu trnh {1, 2, 3}, xa cnh {2, 3}.
G := DeleteEdge(G, { {1, 6}, {1, 5}, {2, 3} } );
DrawGraph(G);
- Cc nh 5, 6 tr thnh nh bc 2, ly cnh {5, 4}, {5, 8}, {6, 7}, {6, 10}.
- Ly cnh {2, 9} c lin thng. Ta c chu trnh { 9, 10, 6, 7, 8, 9 }; Trong ta
ly cnh {6, 7}, {6, 10} vy b cnh {7, 8}.
G := DeleteEdge(G, {7, 8} );
DrawGraph(G);
DrawGraph(G);
Chu trnh 2:
> with(GraphTheory):
with(SpecialGraphs):
G := PetersenGraph();
AddEdge(G, {1, 3});
DrawGraph(G);
IsHamiltonian(G, 'T');
T = { 1, 2, 9, 8, 5, 4, 10, 6, 7, 3, 1 }
- Bt u t nh 1, ly cnh {1, 2}, {1, 3}, xa cnh {1, 6}, {1, 5} (do ly 2
cnh k vi nh 1). Ly {2, 3} s to thnh chu trnh {1, 2, 3}, xa cnh {2, 3}.
G := DeleteEdge(G, { {1, 6}, {1, 5}, {2, 3} } );
DrawGraph(G);
65
- Cc nh 5, 6 tr thnh nh bc 2, ly cnh {5, 4}, {5, 8}, {6, 7}, {6, 10}.
- Ly cnh {2, 9} c lin thng. Ta c chu trnh { 9, 10, 6, 7, 8, 9 }; Trong ta
ly cnh {6, 7}, {6, 10} vy b cnh {8, 9}.
G := DeleteEdge(G, { {1, 6}, {1, 5}, {2, 3}, {8, 9} } );
DrawGraph(G);
66
67
nh 1
nh 3
nh 4
nh 5
nh 6
nh 7
1
2
3
4
5
6
7
Kt qu
(, )
(7, 4)
(7, 4)
(7, 4)
(6, 2)
(6, 2)
(6, 2)
(4, 2)
(6, 2)
(, )
(10, 4)
(10, 4)
(10, 4)
(10, 4)
(7, 4)
(6, 2)
(6, 2)
(10, 4)
(, )
(, )
(14, 3)
(14, 3)
(14, 3)
(14, 3)
(14, 3)
(14, 3)
(, )
(, )
(11, 3)
(11, 3)
(11, 3)
(11, 3)
(11, 3)
68
nh
chn
2
4
3
1
5
7
6
- Bt u t nh 1, cc nh k vi nh 1 l 3, 4, 6, 7. Cnh {1, 4} c di 3 l
min, thm cnh {1, 4} vo T.
- Cc nh k vi nh 1, 4 l 3, 6, 7, 2, 5. Cnh {4, 3} c di 2 l min, thm
cnh {4, 3} vo T.
- Cnh {4, 2} c di 4 l min, thm cnh {4, 2} vo T.
- Cnh {1, 6} c di 5 l min, thm cnh {1, 6} vo T.
- Cnh {3, 7} c di 5 l min, thm cnh {3, 7} vo T.
- Cnh {7, 5} c di 4 l min, thm cnh {7, 5} vo T. 6 cnh.
- T = { {1, 4}, {4, 3}, {4, 2}, {1, 6}, {3, 7}, {7, 5} }
69
'
70