| Structure | Menu |
|---|---|
| Stack | Insert, GetYoungest |
| Queue | Insert, GetOldest |
| Priority Queue | Insert, GetLargest |
| Mergeable Queue | Insert, GetLargest, Union |
| Dictionary | Insert, Delete, Find |
| Partition | Union, FindStructure |
{Stack is implemented as an array Stack[1..stackSize], with youngest element
Stack[stackTop], and oldest Stack[1]; stackSize >= stackTop >= 0.
Initially stackTop = 0 }
Insert(element, Stack)
{ add element to Stack }
if Full(Stack) then
Error("stack is full")
else
stackTop++
Stack[stackTop] = element
Empty (Stack)
{ tests whether Stack is empty }
if stackTop = 0 then
return true
else
return false
{ An implementation of two partition operations. Size[node] = size of the set with
root node. Parent[node] = node if node is the root of the set, otherwise
it is the parent of node. }
FindStructure (node)
{ find the root of the set containing node, then compress the set using
any information gained. }
next = node
while (Parent[next] != next)
next = Parent[next]
root = next
next = node
while (next != root)
save = Parent[next]
Parent[next] = root
next = save
return root
Union(node1, node2)
{ merge the sets containing node1 and node2 }
root1 = FindStructure(node1)
root2 = FindStructure(node2)
if root1 = root2 then return
sum = Size[root1] + Size[root2]
if Size[root1] > Size[root2] then
Parent[root2] = root1
Size[root1] = sum
else
Parent[root1] = root2
Size[root2] = sum
ShortestPath ( A: n x n matrix; x, y : nodes )
{ Dijkstra's algorithm - A is a modified adjacency matrix for a simple, connected graph with positive weights; x and y are nodes in the graph; procedure writes out nodes in the shortest path from x to y, and the distance for that path}
var
IN : set of nodes; {shortest known path from x}
z, p : node;
d[] : array of integers; {distance from x using nodes in IN}
s[] : array of nodes; { previous node in shortest path}
OldDistance : integer;
begin
{initialize set IN and arrays d and s}
IN = {x};
d[x] = 0;
for all nodes z not in IN do
d[z] = A[x,z];
s[z] = x;
{process nodes into IN}
while Y not in IN do
{add minimum distance node not in IN to IN}
p = node z not in IN with minimum d[z];
IN = IN U {p};
{recomputer d for non-IN nodes, adjust s if necessary}
for all nodes z not in IN do
OldDistance = d[z];
d[z] = min (d[z], d[p] + A[p,z]);
if not (d[z] = OldDistance) then
s[z] = p;
{write out path nodes}
write('In reverse order, the path is');
write(y);
z = y;
repeat
write (s[z]);
z = s[z];
until z = x;
write ('The path distance is ', d[y]);
AllShortestPaths ( Graph& G)
{ Floyd's algorithm - G is a simple, connected graph with positive weights; procedure stores all-pair shortest distances in array Dist}
int Dist[G.n()] [G.n()];
{initialize D with the weights from G}
for (int i = 0; i < G.n(); i++)
for (int j = 0; j < G.n(); j++)
D[i][j] = G.weight(i,j);
{compute all k paths}
for (int k = 0; k < G.n(); k++)
for (int i = 0; i < G.n(); i++)
for (int j = 0; j < G.n(); j++)
if (D[i][j] > (D[i][k] + D[k][j]))
D[i][j] = D[i][k] + D[k][j]