<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Population</title>
</head>

<body>



<p align="center"><span lang="en-ca"><font size="6" color="#FF0000"><b>Shortest 
distance</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A.Second Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is second<span lang="en-ca"> edition of fifth</span> assignment of <span lang="en-ca">comp352</span> which is an obvious solution.<span lang="en-ca"> </span></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <pre><b><span lang="en-ca"><font size="3">How to find out distance between two vertices in a simple graph without weight?</font></span></b></pre>
</div>
<div align="left">
  <pre><font size="3"><b>Here is the <a href="GRAPH1.txt">graph input file</a>.</b></font></pre>
</div>
<div align="left">
  <font FACE="CenturySchoolbook-BoldItalic" SIZE="3"><i><b></b></i></font>
</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <b><span lang="en-ca">1. Using BFS. </span></b></div>
<div align="left">
  <b><span lang="en-ca">2. Using my own Matrix class as an input tool because I 
  am too lazy to write again the same routine.</span></b></div>
<div align="left">
  <b><span lang="en-ca">3. Re-write &quot;createGraph&quot; from Mr. shaffer's code 
  because I want to steal his idea.(:</span></b></div>
<div align="left">
  <b><span lang="en-ca">4. Abusively using &quot;mark&quot; as recording the distance from 
  &quot;starting&quot; vertices because I want to confuse the marker. (:</span></b></div>
<div align="left">
  <b><span lang="en-ca">5. I feel regret that I didn't use my own version of &quot;BFS&quot; 
  to finish the program because I forgot to do it.</span></b></div>
<div align="left">
  <b><span lang="en-ca">6. I kept thinking about using &quot;Kruskal&quot; or &quot;Prim&quot; 
  algorithm to make this program even capable to search distance within a 
  &quot;weighted&quot; graph. </span></b></div>
<div align="left">
  <b><span lang="en-ca">7. I should change the code a little bit to enable to 
  output the path. This requires that I modify the graph </span></b></div>
<div align="left">
  <b><span lang="en-ca">class to add one more field to represent the parent of 
  current vertex so that when finding the &quot;end&quot; vertex&quot; we can &quot;backtrack&quot; the 
  path by this information. I have done this in &quot;Kruskal&quot; algorithms because it 
  is said by the instructor of comp239 that &quot;the algorithm will give you the 
  shortest length between two vertex, the shortest path is only a side effect it 
  may give you provided you do the bookkeeping job yourself.&quot; I do miss the 
  teacher even though he is only a part-time instructor and he usually made many 
  mistakes. He is a purely mathematisian. </span></b></div>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">C</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre><b><span lang="en-ca"><font size="3">1. output shortest path.</font></span></b></pre>
</div>
<div align="left">
  <pre><b><span lang="en-ca"><font size="3">2. capable for weighted graph.</font></span></b></pre>
</div>
<div align="left">
  <pre><b><span lang="en-ca"><font size="3">3. Using Kruskal or Prim algorithms.</font></span></b></pre>
</div>
<div align="left">
  <pre>　</pre>
</div>
<div align="left">
  <pre><b><span lang="en-ca"><font size="3" color="#FF0000">//this is main program: shortest.cpp</font></span></b></pre>
</div>
<pre>#include &lt;iostream&gt;
#include &quot;Grlist.h&quot;
#include &quot;Graph.h&quot;
#include &quot;Grmat.h&quot;
#include &quot;Queue.h&quot;
#include &quot;AQueue.h&quot;

using namespace std;

//returns a directed graph as specified
Graph*  getGraph1(int size);

//returns an undirected graph as specified
Graph*  getGraph2(int size);//input 2nd graph

//test two vertices without a connecting edge
Graph* getGraph3(int size);

//Shortest path returns the shortest number of edges 
//needed to cross to get to another vertex, independent of 
//what kind of graph
int shortest(Graph* G, int start, int end);

int main()
{
	Graph* G;

	G=getGraph1(6);
	cout&lt;&lt;&quot;output of graph 1:&quot; &lt;&lt;endl;
	cout&lt;&lt;&quot;The shortest distance between A and E is:&quot;&lt;&lt;shortest(G, 0, 4)&lt;&lt;endl;
	cout&lt;&lt;&quot;The shortest distance between A and C is:&quot;&lt;&lt;shortest(G, 0, 2)&lt;&lt;endl;
	cout&lt;&lt;&quot;The shortest distance between A and D is:&quot;&lt;&lt;shortest(G, 0, 3)&lt;&lt;endl;

	delete G;

	G=getGraph2(6);
	cout&lt;&lt;&quot;output of graph 2:&quot; &lt;&lt;endl;
	cout&lt;&lt;&quot;The shortest distance between A and D is:&quot;&lt;&lt;shortest(G, 0, 3)&lt;&lt;endl;
	cout&lt;&lt;&quot;The shortest distance between A and F is:&quot;&lt;&lt;shortest(G, 0, 5)&lt;&lt;endl;
	cout&lt;&lt;&quot;The shortest distance between C and F is:&quot;&lt;&lt;shortest(G, 2, 5)&lt;&lt;endl;
	delete G;

	G=getGraph3(6);
	cout&lt;&lt;&quot;output of graph 3:&quot; &lt;&lt;endl;
	cout&lt;&lt;&quot;The shortest distance between A and E is:&quot;&lt;&lt;shortest(G, 0, 4)&lt;&lt;endl;
	cout&lt;&lt;&quot;The shortest distance between A and C is:&quot;&lt;&lt;shortest(G, 0, 2)&lt;&lt;endl;
	cout&lt;&lt;&quot;The shortest distance between A and D is:&quot;&lt;&lt;shortest(G, 0, 3)&lt;&lt;endl;
	
	delete G;
	return 0;
}

Graph* getGraph1(int size)
{
	Graph* G=new Graphm(size);
	G-&gt;setEdge(0,2,1);
	G-&gt;setEdge(2,1,1);
	G-&gt;setEdge(1,5,1);
	G-&gt;setEdge(5,3,1);
	G-&gt;setEdge(5,4,1);

	return G;
}


//in an undirected graph an edge is defined by the vertices 
//twice(A to C, and C to A)
Graph*  getGraph2(int size)
{
	Graph* G=new Graphm(size);
	G-&gt;setEdge(0,2,1);
	G-&gt;setEdge(2,0,1);

	G-&gt;setEdge(2,3,1);
	G-&gt;setEdge(3,2,1);

	G-&gt;setEdge(1,2,1);
	G-&gt;setEdge(2,1,1);

	G-&gt;setEdge(5,2,1);
	G-&gt;setEdge(2,5,1);

	G-&gt;setEdge(1,5,1);
	G-&gt;setEdge(5,1,1);

	G-&gt;setEdge(5,3,1);
	G-&gt;setEdge(3,5,1);

	G-&gt;setEdge(0,4,1);
	G-&gt;setEdge(4,0,1);


	G-&gt;setEdge(5,4,1);
	G-&gt;setEdge(4,5,1);

	return G;
}

Graph* getGraph3(int size)
{
	//same graph as graph 1 except the edge between B and F is removed
	Graph* G=new Graphm(size);
	G-&gt;setEdge(0,2,1);
	G-&gt;setEdge(2,1,1);
	G-&gt;setEdge(5,3,1);
	G-&gt;setEdge(5,4,1);

	return G;
}


int shortest(Graph* pG, int start, int end)
{
	int p, q;
	int level=0;

	// all vertices are unchecked
	for (int i=0; i&lt;pG-&gt;n(); i++)
	{
		pG-&gt;setMark(i, -1);
	}
	AQueue&lt;int&gt; Q;
	Q.enqueue(start);
	
	pG-&gt;setMark(start, level);
	
	while (Q.length()!=0)
	{
		Q.dequeue(p);
		if (p==end)
		{
			return pG-&gt;getMark(p);//the level
		}
		level = pG-&gt;getMark(p)+1; //add 1 to get the correct level
		for (q=pG-&gt;first(p); q&lt;pG-&gt;n(); q=pG-&gt;next(p, q))
		{
			if (pG-&gt;getMark(q)==-1)
			{
				pG-&gt;setMark(q, level);
				Q.enqueue(q);
			}
		}
	}
	return -1;
}</pre>
<pre>　</pre>
<pre><b><span lang="en-ca"><font size="3" color="#FF0000">//this is AQueue.h</font></span></b></pre>
<pre>#ifndef AQUEUE_H
#define AQUEUE_H
// This is the file to include in your code if you want access to the
// complete AQueue template class

// First, get the declaration for the base stack class
#include &quot;queue.h&quot;

// Array-based queue implementation
template &lt;class Elem&gt; class AQueue: public Queue&lt;Elem&gt; {
private:
  int size;                  // Maximum size of queue
  int front;                 // Index of front element
  int rear;                  // Index of rear element
  Elem *listArray;           // Array holding queue elements
public:
  AQueue(int sz =DefaultListSize) { // Constructor 
    // Make list array one position larger for empty slot
    size = sz+1;
    rear = 0;  front = 1;
    listArray = new Elem[size];
  }
  ~AQueue() { delete [] listArray; } // Destructor
  void clear() { front = rear; }
  bool enqueue(const Elem&amp; it) {
    if (((rear+2) % size) == front) return false;  // Full
    rear = (rear+1) % size; // Circular increment
    listArray[rear] = it;
    return true;
  }
  bool dequeue(Elem&amp; it) {
    if (length() == 0) return false;  // Empty
    it = listArray[front];
    front = (front+1) % size; // Circular increment
    return true;
  }
  bool frontValue(Elem&amp; it) const {
    if (length() == 0) return false;  // Empty
    it = listArray[front];
    return true;
  }
  virtual int length() const
   { return ((rear+size) - front + 1) % size; }
};

#endif</pre>
<pre><b><span lang="en-ca"><font size="3" color="#FF0000">//this is base class Queue----Queue.h</font></span></b></pre>
<pre><span lang="en-ca">#ifndef QUEUE_H
#define QUEUE_H

// Abstract queue class
template &lt;class Elem&gt; class Queue {
public:
// Reinitialize the queue. The user is responsible for
// reclaiming the storage used by the stack elements.
virtual void clear() = 0;
// Place an element at the rear of the queue. Return
// true if successful, false if not (if queue is full).
virtual bool enqueue(const Elem&amp;) = 0;
// Remove the element at the front of the queue. Return
// true if succesful, false if queue is empty.
// The element removed is returned in the first parameter.
virtual bool dequeue(Elem&amp;) = 0; // Remove Elem from front
// Return in first parameter a copy of the front element.
// Return true if succesful, false if queue is empty.
virtual bool frontValue(Elem&amp;) const = 0;
// Return the number of elements in the queue.
virtual int length() const = 0;
};

#endif</span></pre>
<pre><b><font color="#FF0000" size="3"><span lang="en-ca">//this is link.h</span></font></b></pre>
<pre>#ifndef LINK_H
#define LINK_H

// Singly-linked list node
template &lt;class Elem&gt; class Link {
public:
  Elem element;      // Value for this node
  Link *next;        // Pointer to next node in list
  Link(const Elem&amp; elemval, Link* nextval =NULL)
    { element = elemval;  next = nextval; }
  Link(Link* nextval =NULL) { next = nextval; }
};

#endif</pre>
<pre><b><span lang="en-ca"><font size="3" color="#FF0000">//this is list.h</font></span></b></pre>
<pre>#ifndef LIST_H
#define LIST_H

// List abstract class
template &lt;class Elem&gt; class List {
public:
  // Reinitialize the list.  The client is responsible for
  // reclaiming the storage used by the list elements.
  virtual void clear() = 0;
  // Insert an element at the front of the right partition.
  // Return true if successful, false if the list is full.
  virtual bool insert(const Elem&amp;) = 0;
  // Append an element at the end of the right partition.
  // Return true if successful, false if the list is full.
  virtual bool append(const Elem&amp;) = 0;
  // Remove the first element of right partition. Return
  // true if successful, false if right partition is empty.
  // The element removed is returned in the parameter.
  virtual bool remove(Elem&amp;) = 0;
  // Place fence at list start, making left partition empty
  virtual void setStart() = 0;
  // Place fence at list end, making right partition empty
  virtual void setEnd() = 0;
  // Move fence one step left; no change if already at start
  virtual void prev() = 0;
  // Move fence one step right; no change if already at end
  virtual void next() = 0;
  // Return length of left partition
  virtual int leftLength() const = 0;
  // Return length of right partition
  virtual int rightLength() const = 0;
  // If pos or more elements are in the list, set the size
  // of left partition to pos and return true.  Otherwise,
  // do nothing and return false.
  virtual bool setPos(int pos) = 0;
  // Return in first parameter the first element  of the
  // right partition.  Return true if successful, false
  // if the right partition is empty.
  virtual bool getValue(Elem&amp;) const = 0;
  // Print the contents of the list
  virtual void print() const = 0;
};

#endif</pre>
<pre><b><span lang="en-ca"><font size="3" color="#FF0000">//this is llist.h (the link-list)</font></span></b></pre>
<pre>#ifndef LLIST_H
#define LLIST_H

// This is the file to include in your code if you want access to the
// complete LList template class

// First, get the declaration for the base list class
#include &quot;list.h&quot;

const int DefaultListSize=50;

// Linked list implementation
template &lt;class Elem&gt; class LList: public List&lt;Elem&gt; {
private:
  Link&lt;Elem&gt;* head;       // Pointer to list header
  Link&lt;Elem&gt;* tail;       // Pointer to last Elem in list 
  Link&lt;Elem&gt;* fence;      // Last element on left side
  int leftcnt;            // Size of left partition
  int rightcnt;           // Size of right partition
  void init() {           // Intialization routine
    fence = tail = head = new Link&lt;Elem&gt;;
    leftcnt = rightcnt = 0;
  }
  void removeall() {   // Return link nodes to free store
    while(head != NULL) {
      fence = head;
      head = head-&gt;next;
      delete fence;
    }
  }
public:
  LList(int size=DefaultListSize) { init(); }
  ~LList() { removeall(); }  // Destructor
  void clear() { removeall(); init(); }
  bool insert(const Elem&amp;);
  bool append(const Elem&amp;);
  bool remove(Elem&amp;);
  void setStart()
    { fence = head; rightcnt += leftcnt; leftcnt = 0; }
  void setEnd()
    { fence = tail; leftcnt += rightcnt; rightcnt = 0; }
  void prev();
  void next() {
    if (fence != tail) // Don't move fence if right empty
      { fence = fence-&gt;next; rightcnt--; leftcnt++; }
  }
  int leftLength() const  { return leftcnt; }
  int rightLength() const { return rightcnt; }
  bool setPos(int pos);
  bool getValue(Elem&amp; it) const {
    if(rightLength() == 0) return false;
    it = fence-&gt;next-&gt;element;
    return true;
  }
  void print() const;
};

template &lt;class Elem&gt; // Insert at front of right partition
bool LList&lt;Elem&gt;::insert(const Elem&amp; item) {
  fence-&gt;next = new Link&lt;Elem&gt;(item, fence-&gt;next);  
  if (tail == fence) tail = fence-&gt;next;  // New tail
  rightcnt++;
  return true;
}

template &lt;class Elem&gt; // Append Elem to end of the list
bool LList&lt;Elem&gt;::append(const Elem&amp; item) {
  tail = tail-&gt;next = new Link&lt;Elem&gt;(item, NULL);
  rightcnt++;
  return true;
}

// Remove and return first Elem in right partition
template &lt;class Elem&gt; bool LList&lt;Elem&gt;::remove(Elem&amp; it) {
  if (fence-&gt;next == NULL) return false; // Empty right
  it = fence-&gt;next-&gt;element;       // Remember value
  Link&lt;Elem&gt;* ltemp = fence-&gt;next; // Remember link node
  fence-&gt;next = ltemp-&gt;next;       // Remove from list
  if (tail == ltemp) tail = fence; // Reset tail
  delete ltemp;                    // Reclaim space
  rightcnt--;
  return true;
}

// Move fence one step left; no change if left is empty
template &lt;class Elem&gt; void LList&lt;Elem&gt;::prev() {
  Link&lt;Elem&gt;* temp = head;
  if (fence == head) return; // No previous Elem
  while (temp-&gt;next!=fence) temp=temp-&gt;next;
  fence = temp;
  leftcnt--; rightcnt++;
}

// Set the size of left partition to pos
template &lt;class Elem&gt; bool LList&lt;Elem&gt;::setPos(int pos) {
  if ((pos &lt; 0) || (pos &gt; rightcnt+leftcnt)) return false;
  fence = head;
  for(int i=0; i&lt;pos; i++) fence = fence-&gt;next;
  return true;
}

template &lt;class Elem&gt; void LList&lt;Elem&gt;::print() const {
  Link&lt;Elem&gt;* temp = head;
  cout &lt;&lt; &quot;&lt; &quot;;
  while (temp != fence) {
    cout &lt;&lt; temp-&gt;next-&gt;element &lt;&lt; &quot; &quot;;
    temp = temp-&gt;next;
  }
  cout &lt;&lt; &quot;| &quot;;
  while (temp-&gt;next != NULL) {
    cout &lt;&lt; temp-&gt;next-&gt;element &lt;&lt; &quot; &quot;;
    temp = temp-&gt;next;
  }
  cout &lt;&lt; &quot;&gt;\n&quot;;
}


#endif</pre>
<pre>　</pre>
<pre><b><span lang="en-ca"><font size="3" color="#FF0000">//this is graph.h</font></span></b></pre>
<pre>#ifndef GRAPH_H
#define GRAPH_H

// Graph abstract class
class Graph {
public:
  // Return the number of vertices
  virtual int n() =0;
  // Return the current number of edges
  virtual int e() =0;
  // Return the index of the first neigbor of given vertex
  virtual int first(int) =0;
  // Return the index of the next neigbor of given vertex
  virtual int next(int, int) =0;
  // Store an edge defined by two vertices and weight
  virtual void setEdge(int, int, int) =0;
  // Delete edge defined by two vertices
  virtual void delEdge(int, int) =0;
  // Return weight of edge connecting two vertices
  // Return 0 if no such edge exists
  virtual int weight(int, int) =0;
  // Get mark value for a vertex
  virtual int getMark(int) =0;
  //  Set mark value for a vertex
  virtual void setMark(int, int) =0;

};

#endif</pre>
<pre><b><span lang="en-ca"><font size="3" color="#FF0000">//this is grlist.h</font></span></b></pre>
<pre>#ifndef GRLIST_H
#define GRLIST_H
#include &lt;iostream&gt;

using namespace std;

// Used by the mark array
#define UNVISITED 0
#define VISITED 1

#include &quot;link.h&quot;
#include &quot;llist.h&quot;

#include &quot;graph.h&quot;


class Edge {
public:
  int vertex, weight;
  Edge() { vertex = -1; weight = -1; }
  Edge(int v, int w) { vertex = v; weight = w; }
};

// Overload for the Edge &lt;&lt; operator
ostream&amp; operator &lt;&lt; (ostream&amp; s, Edge e)
  { return(s &lt;&lt; &quot;(&quot; &lt;&lt; e.vertex &lt;&lt; &quot;, &quot; &lt;&lt; e.weight &lt;&lt; &quot;)&quot;); }

class Graphl : public Graph { // Implement adjacency list
private:
  int numVertex, numEdge;     // Number of vertices, edges
  List&lt;Edge&gt;** vertex; // List headers
  int *mark;                  // Pointer to mark array
public:
  Graphl(int numVert) { // Make graph with numVert vertices
    int i;
    numVertex = numVert;  numEdge = 0;
    mark = new int[numVert]; // Initialize mark array
    for (i=0; i&lt;numVertex; i++) mark[i] = UNVISITED;
    // Create and initialize adjacency lists
    vertex = (List&lt;Edge&gt;**) new List&lt;Edge&gt;*[numVertex];
    for (i=0; i&lt;numVertex; i++)
      vertex[i] = new LList&lt;Edge&gt;();
  }

  ~Graphl() {       // Destructor
    delete [] mark; // Return dynamically allocated memory
    for (int i=0; i&lt;numVertex; i++) delete [] vertex[i];
    delete [] vertex;
  }

  int n() { return numVertex; } // Number of vertices
  int e() { return numEdge; }   // Number of edges

  int first(int v) { // Return first neighbor of v
    Edge it;
    vertex[v]-&gt;setStart();
    if (vertex[v]-&gt;getValue(it)) return it.vertex;
    else return numVertex;  // Return n if none
  }

  int next(int v1, int v2) { // Gete v1's neighbor after v2
    Edge it;
    vertex[v1]-&gt;getValue(it);
    if (it.vertex == v2) vertex[v1]-&gt;next();
    else { // Start from beginning of list
      vertex[v1]-&gt;setStart();
      while (vertex[v1]-&gt;getValue(it) &amp;&amp; (it.vertex &lt;= v2))
        vertex[v1]-&gt;next();
    }
    if (vertex[v1]-&gt;getValue(it)) return it.vertex;
    else return numVertex;  // Return n if none
  }

  // Set edge (v1, v2) to wgt
  void setEdge(int v1, int v2, int wgt) {
//   Assert(wgt&gt;0, &quot;Illegal weight value&quot;);
    Edge it(v2, wgt);
    Edge curr;
    vertex[v1]-&gt;getValue(curr);
    if (curr.vertex != v2) // If not already there, search
      for (vertex[v1]-&gt;setStart();
           vertex[v1]-&gt;getValue(curr); vertex[v1]-&gt;next())
        if (curr.vertex &gt;= v2) break;
    if (curr.vertex == v2)  // Clear out the current one
      vertex[v1]-&gt;remove(curr);
    else numEdge++;
    vertex[v1]-&gt;insert(it);
  }

  void delEdge(int v1, int v2) { // Delete edge (v1, v2)
    Edge curr;
    vertex[v1]-&gt;getValue(curr);
    if (curr.vertex != v2)  // If not already there, search
      for (vertex[v1]-&gt;setStart();
           vertex[v1]-&gt;getValue(curr); vertex[v1]-&gt;next())
        if (curr.vertex &gt;= v2) break;
    if (curr.vertex == v2) {  // If not, then there is none
      vertex[v1]-&gt;remove(curr);
      numEdge--;
    }
  }

  int weight(int v1, int v2) { // Return weight of (v1, v2)
    Edge curr;
    vertex[v1]-&gt;getValue(curr);
    if (curr.vertex != v2) // If not already there, search
      for (vertex[v1]-&gt;setStart();
           vertex[v1]-&gt;getValue(curr); vertex[v1]-&gt;next())
        if (curr.vertex &gt;= v2) break;
    if (curr.vertex == v2)
      return curr.weight;
    else
      return 0;  // No such edge
  }

  int getMark(int v) { return mark[v]; }
  void setMark(int v, int val) { mark[v] = val; }
};

#endif
</pre>
<pre><b><span lang="en-ca"><font size="3" color="#FF0000">//this is grmat.h</font></span></b></pre>
<pre>#ifndef GRMAT_H
#define GRMAT_H


// Used by the mark array
#define UNVISITED 0
#define VISITED 1

#include &quot;graph.h&quot;

class Graphm : public Graph { // Implement adjacency matrix
private:
  int numVertex, numEdge; // Store number of vertices, edges
  int **matrix;           // Pointer to adjacency matrix
  int *mark;              // Pointer to mark array
public:
  Graphm(int numVert) {   // Make graph w/ numVert vertices
    int i;
    numVertex = numVert;
    numEdge = 0;
    mark = new int[numVert]; // Initialize mark array
    for (i=0; i&lt;numVertex; i++)
      mark[i] = UNVISITED;
    matrix = (int**) new int*[numVertex]; // Make matrix
    for (i=0; i&lt;numVertex; i++)
      matrix[i] = new int[numVertex];
    for (i=0; i&lt; numVertex; i++) // Edges start w/ 0 weight
      for (int j=0; j&lt;numVertex; j++) matrix[i][j] = 0;
  }

  ~Graphm() {       // Destructor
    delete [] mark; // Return dynamically allocated memory
    for (int i=0; i&lt;numVertex; i++)
      delete [] matrix[i];
    delete [] matrix;
  }

  int n() { return numVertex; } // Number of vertices
  int e() { return numEdge; }   // Number of edges

  int first(int v) {            // Return v's first neighbor
    int i;
    for (i=0; i&lt;numVertex; i++)
      if (matrix[v][i] != 0) return i;
    return i;       // Return n if none
  }

  int next(int v1, int v2) { // Get v1's neighbor after v2
    int i;
    for(i=v2+1; i&lt;numVertex; i++)
      if (matrix[v1][i] != 0) return i;
    return i;
  }

  // Set edge (v1, v2) to wgt
  void setEdge(int v1, int v2, int wgt) {
//    Assert(wgt&gt;0, &quot;Illegal weight value&quot;);
    if (matrix[v1][v2] == 0) numEdge++;
    matrix[v1][v2] = wgt;
  }

  void delEdge(int v1, int v2) { // Delete edge (v1, v2)
    if (matrix[v1][v2] != 0) numEdge--;
    matrix[v1][v2] = 0;
  }

  int weight(int v1, int v2) { return matrix[v1][v2]; }
  int getMark(int v) { return mark[v]; }
  void setMark(int v, int val) { mark[v] = val; }
};

#endif
</pre>
<pre>　</pre>
<pre><font color="#FF0000"><b><a name="input"></a><a name="input" href="picture/graphSimple.JPG">Here is what the graph looks like.</a></b></font></pre>
<pre>　</pre>
<pre><font color="#0000FF"><b>Here is the result:</b></font></pre>
<pre><b>output of graph 1:
The shortest distance between A and E is:4
The shortest distance between A and C is:1
The shortest distance between A and D is:4
output of graph 2:
The shortest distance between A and D is:2
The shortest distance between A and F is:2
The shortest distance between C and F is:1
output of graph 3:
The shortest distance between A and E is:-1
The shortest distance between A and C is:1
The shortest distance between A and D is:-1
Press any key to continue</b></pre>
<pre>　</pre>
<pre>　</pre>
<pre></pre>
<pre>　</pre>

<pre></pre>

<pre></pre>
<pre>	
</pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="WhoAmI.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>