Start of Class Tuesday, Nov 14, 2000
Here are some examples (not the best) from Lab Assignment 5.
-----------------------------------------------------
struct hashentry *hashinit(){
struct hashentry entryp[HASHSIZE];
for (i=0;i<HASHSIZE;i++){
entryp[i] = (struct hashentry *) malloc (sizeof (struct hashentry));
entryp[i]->status = HASH_STATUS_EMPTY;
}
return entryp;
}
-----------------------------------------------------
struct hashentry hashtable[HASHSIZE];
struct hashentry *hashinit() {
int i;
for(i=0;i<HASHSIZE;i++)
hashtable[i].status=HASH_STATUS_EMPTY;
return hashtable;
}
-----------------------------------------------------
struct hashentry *hashinit()
{
static struct hashentry hashTable[HASHSIZE];
int i;
for( i = 0 ; i < HASHSIZE ; i++)
{
hashTable[i].sval.str = NULL;
hashTable[i].sval.val = -1;
hashTable[i].status = HASH_STATUS_EMPTY;
}
return hashTable;
}
-----------------------------------------------------
struct hashentry *hashinit(){
struct hashentry *p;
p = (struct hashentry *) malloc(sizeof(struct hashentry));
p->status = 0;
p->sval.str = "\0";
p->sval.val = 0;
return p;
}
-----------------------------------------------------
struct hashentry *hashinit() {
int size = HASHSIZE*sizeof(struct hashentry);
struct hashentry *p;
p = (struct hashentry*)malloc(size);
if (p) memset(p,0,size);
return p;
}
-----------------------------------------------------
Graphs and graph algorithms will be coever in Analysis of Algorithms. Here, we are not interested int he theory, but in the implementation of one of the standard algorithms, Dijkstra's Algorithm.
You can think of a directed weighted graph as a collection of nodes (places) and directed edges (paths) in which the edges are labeled with a weight.
One application is in computer networks, where each node is a machine and each edge represents a direct connection between machines. The wieght can represent the delay in sending a piece of information from one machine to another.
As an example, consider the graph below:
Machines A though H are connected by communication channels labeled by the delay in sending information from one machine to another. Note that these are directional and in a real system there would be edges in both directions, possibly with different weights.
To send a message from A to H there are many possibilities:
A -> B -> C -> H total = 26 A -> B -> G -> D -> C -> H total = 40 A -> F -> C -> H total = 20There are other paths that contain loops:
A -> B -> G -> F -> C -> G -> F -> C -> H total= 46Here is one algorithm for finding the cost of the shortest path from A to each of the other nodes:
Let nodes be the set of nodes we have not done yet and let distances be a list of our best estimate of the cost from node A to each of the other nodes.
We initialize nodes to contain nodes B-H and distances will contain estimates based on direct connections only:
B:5 C:inf D:inf E:2 F:10 G:inf H:infThis is based on direct connections only.
First we find the node in nodes which is closest to A.
In our example this is node E.
This one will have to be reached with a direct route.
We remove E from nodes and adjust the values in distances
by looking at possible paths through E.
In this case there is no path to any other node from A through E
so we continue.
At this point we have:
nodes: B C D F G H
distances: B:5 C:inf D:inf E:2 F:10 G:inf H:inf
We now look through distances for nodes in nodes and find the
smallest entry, in this case B.
We remove B from nodes and adjust the distances for all nodes
in nodes.
For each node, X, in nodes we compare the direct distance from A to X
with the sum of the distance from A to B and B to X.
Two values need to be adjusted: A -> C is now 20 and A -> G is node 6.
At this point we have:
nodes: C D F G H
distances: B:5 C:20 D:inf E:2 F:10 G:6 H:inf
We keep doing this until nodes is empty.
The next node to be removed is G.
The following are the required operations on the list of adjacencies:
int init(int num); int addadjacency(int source, int dest, int cost); int findcost(int source, int dest); void printall(void);The last of these is for testing and debugging.
You will implement these using an array of linked lists in a file called
adjacency.c.
The implmentation will be hidden from the main program which will only know
about the prototypes listed above.
You will use the following typedefs inside adjacency.c
typedef struct {
int node;
int cost;
} edge;
typedef struct entry {
edge e;
struct entry *next;
} entry;
typedef entry **adjacencies;
The adjacencies should be considered an array of pointers to
entry structures. Each entry corresponds to the nodes adjacent to a given node.