Проектирование программной коллекции "Простой, статический граф" - Курсовая работа

бесплатно 0
4.5 120
Разработка и реализация универсальной программной коллекции для абстрактного типа данных (АТД) "Простой статический граф" с графическим интерфейсом. Особенности использование ее для решения задач для неориентированных, ориентированных и взвешенных графов.


Аннотация к работе
Интерфейс АТД «Простой, статический граф» включает операции: Конструктор () по умолчанию: создает пустой L - граф с нулевым числом вершин и ребер, Конструктор(V, D, F) создает граф с V вершинами, без ребер, типа D(ориентированный / неориентированный), формы представления F(L-граф/M-граф), Конструктор(V, E, D, F) создает граф с V вершинами, с E случайными ребрами, типа Dense( ) - возвращает форму представления графа (L-граф / M-граф), K( ) - возвращает коэффициент насыщенности графа, TOLISTGRAPH()преобразует граф к L-графу, TOMATRIXGRAPH()преобразует граф к M-графу, INSERTV(v)добавляет к графу вершину c заданным номером v, DELETEV (v)-удаляет из графа вершину c заданным номером v, INSERTE(v1, v2) - добавляет ребро (v1, v2) к графу, соединяющую вершины, заданные номерами v1 и v2, DELETEE (v1, v2)удаляет ребра (v1, v2), соединяющего вершины, заданные номерами v1 и v2, Is_Edge(v1, v2)возвращает признак существования в графе ребра соединяющего вершины, заданные номерами v1 и v2, GETEDGEWEIGHT(v1,v2)возвращает вес ребра (v1, v2), соединяющего вершины, заданные номерами v1 и v2, SETEDGEWEIGHT (v1,v2, w)задает вес ребра (v1, v2), соединяющего вершины, заданные номерами v1 и v2, равным w. Интерфейс АТД «Итератор ребер графа» включает операции: Конструктор () - создает итератор ребер графа, beg( ) - возвращает итератор, установленный на первое ребро графа, end( ) - возвращает итератор, соответствующий окончанию переходов итератора, operator - переход к следующему ребру графа, operator * - возвращает дескриптор ребра графа, на которое указывает итератор. Интерфейс АТД «Итератор исходящих ребер вершины» включает операции: Конструктор (v) - создает итератор исходящих ребер графа для вершины, заданной номером v, beg( ) - возвращает итератор, установленный на первое исходящее ребро вершины, end( ) - возвращает итератор, соответствующий окончанию переходов итератора, operator - переход к следующему исходящему ребру, operator * - возвращает дескриптор исходящего ребра вершины, на которое указывает итератор. Интерфейс АТД «Итератор входящих ребер вершины» включает операции: Конструктор (v) - создает итератор входящих ребер графа для вершины, заданной номером v, beg( ) - возвращает итератор, установленный на первое входящее ребро вершины, end( ) - возвращает итератор, соответствующий окончанию переходов итератора, operator - переход к следующему входящему ребру, operator * - возвращает дескриптор входящего ребра вершины, на которое указывает итератор..

Список литературы
1. Р. Сэджвик, «Фундаментальные алгоритмы на C . Часть 5. Алгоритмы на графах», 2002 г. - 496 с.

Приложение

Исходные коды

Edge.cs using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace RGR

{ public class Edge : ICLONEABLE

{ private int vertex1;

private int vertex2;

private W weight;

private D data;

public Edge()

{ vertex1 = -1;

vertex2 = -1;

} public Edge(int v1, int v2)

{ vertex1 = v1;

vertex2 = v2;

} public Edge(int v1, int v2, W w)

{ vertex1 = v1;

vertex2 = v2;

weight = w;

} public Edge(int v1, int v2, W w, D d)

{ vertex1 = v1;

vertex2 = v2;

weight = w;

data = d;

} public Object Clone()

{ return new Edge(Vertex1, Vertex2, Weight, Data);

} public int Vertex1

{ set

{ vertex1 = value;

} get

{ return vertex1;

}

} public int Vertex2

{ set

{ vertex2 = value;

} get

{ return vertex2;

}

} public W Weight

{ set

{ weight =value;

} get

{ return weight;

}

} public D Data

{ set

{ data = value;

} get

{ return data;

}

}

}

}

Graph.cs using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace RGR

{ public abstract class Graph

{ public static int MAXVERTICESCOUNT = 10;

private int VERTICESCOUNT;

private int EDGESCOUNT;

private bool directed;

public SORTEDDICTIONARY dictionary;

public Graph()

{

VERTICESCOUNT = 0;

EDGESCOUNT = 0;

directed = false;

} public Graph(int CVERTEX, bool CDIRECTED)

{

VERTICESCOUNT = CVERTEX;

EDGESCOUNT = 0;

directed = CDIRECTED;

} public Graph(int CVERTEX, int CEDGE, bool CDIRECTED)

{

VERTICESCOUNT = CVERTEX;

EDGESCOUNT = CEDGE;

directed = CDIRECTED;

} public int GETVERTICESCOUNT()

{ return VERTICESCOUNT;

} public void INCREASEVERTICESCOUNT()

{

VERTICESCOUNT ;

} public void DECREASEVERTICESCOUNT()

{

VERTICESCOUNT-;

} public int GETEDGESCOUNT()

{ return EDGESCOUNT;

} public void INCREASEEDGESCOUNT()

{

EDGESCOUNT ;

} public void DECREASEEDGESCOUNT()

{

EDGESCOUNT-;

} public void DECREASEEDGESCOUNT(int decrease)

{

EDGESCOUNT -= decrease;

} public bool ISDIRECTED()

{ return directed;

} abstract public bool GETTYPE();

public double DENSECOEF()

{ if (directed) return (double)EDGESCOUNT / ((double)VERTICESCOUNT * ((double)VERTICESCOUNT - 1.0));

return 2.0 * (double)EDGESCOUNT / ((double)VERTICESCOUNT * ((double)VERTICESCOUNT - 1.0));

} abstract public Graph TOLISTGRAPH();

abstract public Graph TOMATRIXGRAPH();

abstract public bool ADDVERTEX(int VERTEXNUMBER);

abstract public bool REMOVEVERTEX(int VERTEXNUMBER);

abstract public bool ADDEDGE(int vertex1, int vertex2);

abstract public bool ADDEDGE(Edge edge);

abstract public bool REMOVEEDGE(int vertex1, int vertex2);

abstract public bool HASEDGE(int vertex1, int vertex2);

abstract public W GETEDGEWEIGHT(int vertex1, int vertex2);

abstract public bool SETEDGEWEIGHT(int vertex1, int vertex2, W weight);

abstract public D GETEDGEDATA(int vertex1, int vertex2);

abstract public bool SETEDGEDATA(int vertex1, int vertex2, D data);

abstract public List GETNEIGHBORS(int VERTEXNUMBER);

//---PARENT ITERATORS-------------------------------- public abstract class Iterator

{ public Graph graph;

public Edge CURRENTPOSITION;

public Iterator(Graph GRAPHC)

{ graph = GRAPHC;

begin();

} public abstract void begin();

public abstract void end();

public abstract void next();

public abstract Edge state();

}

}

}

LGRAPH.cs using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace RGR

{ class LGRAPH : Graph, ICLONEABLE

{ private List>> ADJACENCYLIST;

public LGRAPH()

: base()

{

ADJACENCYLIST = new List>>();

dictionary = new SORTEDDICTIONARY();

} public LGRAPH(int CVERTEX, bool CDIRECTED)

: base(CVERTEX, CDIRECTED)

{ dictionary = new SORTEDDICTIONARY();

ADJACENCYLIST = new List>>(CVERTEX);

for (int i = 0; i < CVERTEX; i )

{

ADJACENCYLIST.Add(new List>());

dictionary.Add(i, i);

}

} public LGRAPH(int CVERTEX, int CEDGE, bool CDIRECTED)

: base(CVERTEX, CDIRECTED)

{

ADJACENCYLIST = new List>>(CVERTEX);

dictionary = new SORTEDDICTIONARY();

for (int i = 0; i < CVERTEX; i )

{

ADJACENCYLIST.Add(new List>());

dictionary.Add(i, i);

}

Random rand = new Random();

for (int i = 0; i < CEDGE; )

{ int vertex1 = rand.Next(CVERTEX);

int vertex2 = rand.Next(CVERTEX);

bool EDGEINSERTED = ADDEDGE(vertex1, vertex2);

if (EDGEINSERTED) i ;

}

} public LGRAPH(int CVERTEX, int CEDGE, List>> list, bool CDIRECTED)

: base(CVERTEX, CEDGE, CDIRECTED)

{

ADJACENCYLIST = list;

} public override bool GETTYPE()

{ return true;

} public override bool ADDVERTEX(int VERTEXNUMBER)

{ if (VERTEXNUMBER < 0) return false;

dictionary.Add(VERTEXNUMBER, ADJACENCYLIST.Count);

ADJACENCYLIST.Add(new List>());

INCREASEVERTICESCOUNT();

return true;

} public override bool REMOVEVERTEX(int VERTEXNUMBER)

{ if (VERTEXNUMBER < 0) return false;

else

{ int DELETEINDEX = dictionary[VERTEXNUMBER];

DECREASEEDGESCOUNT(ADJACENCYLIST[DELETEINDEX].Count);

ADJACENCYLIST.REMOVEAT(DELETEINDEX);

DECREASEVERTICESCOUNT();

foreach (List> list in ADJACENCYLIST)

{ for (int i = 0; i < list.Count; )

{ if (list[i].Vertex2 == DELETEINDEX)

{ list.REMOVEAT(i);

DECREASEEDGESCOUNT();

} else

{ if (list[i].Vertex1 > DELETEINDEX) list[i].Vertex1-;

if (list[i].Vertex2 > DELETEINDEX) list[i].Vertex2-;

i ;

}

}

} dictionary.Remove(VERTEXNUMBER);

for (int key = 0; key <= MAXVERTICESCOUNT; key )

{ int value;

if (dictionary.TRYGETVALUE(key, out value))

{ if (value > VERTEXNUMBER)

{ dictionary.Remove(key);

dictionary.Add(key, value - 1);

}

}

} return true;

}

} public override bool ADDEDGE(int vertex1, int vertex2)

{ if (vertex1 == vertex2 || !dictionary.CONTAINSKEY(vertex1) || !dictionary.CONTAINSKEY(vertex2)) return false;

if (HASEDGE(vertex1, vertex2)) return false;

int source = dictionary[vertex1];

int destanation = dictionary[vertex2];

Edge NEWEDGE = new Edge(source, destanation);

bool inserted = false;

if (source < 0 || destanation < 0 || source == destanation) return false;

if (ADJACENCYLIST[source].Count == 0)

{

ADJACENCYLIST[source].Add(NEWEDGE);

INCREASEEDGESCOUNT();

inserted = true;

} else

{ foreach (Edge edge in ADJACENCYLIST[source])

{ if (edge.Vertex1 == source && edge.Vertex2 == destanation) return false;

}

ADJACENCYLIST[source].Add(NEWEDGE);

INCREASEEDGESCOUNT();

inserted = true;

} if (inserted && !ISDIRECTED())

{

Edge REVERSEEDGE = new Edge(destanation, source);

ADJACENCYLIST[destanation].Add(REVERSEEDGE);

} return inserted;

} public override bool ADDEDGE(Edge edge)

{ if (edge == null) return false;

ADJACENCYLIST[edge.Vertex1].Add(edge);

INCREASEEDGESCOUNT();

if (!ISDIRECTED())

{

Edge REVERSEEDGE = new Edge(edge.Vertex2, edge.Vertex1, edge.Weight, edge.Data);

ADJACENCYLIST[edge.Vertex2].Add(REVERSEEDGE);

} return true;

} public override bool REMOVEEDGE(int vertex1, int vertex2)

{ if (vertex1 == vertex2 || !dictionary.CONTAINSKEY(vertex1) || !dictionary.CONTAINSKEY(vertex2)) return false;

if (!HASEDGE(vertex1, vertex2)) return false;

int source = dictionary[vertex1];

int destanation = dictionary[vertex2];

if (source > GETVERTICESCOUNT() - 1 || destanation > GETVERTICESCOUNT() - 1 || source < 0 || destanation < 0) return false;

for (int i = 0; i < ADJACENCYLIST[source].Count; i ) if (ADJACENCYLIST[source][i].Vertex2 == destanation)

{

ADJACENCYLIST[source].REMOVEAT(i);

DECREASEEDGESCOUNT();

if (!ISDIRECTED())

{ for (int j = 0; j < ADJACENCYLIST[destanation].Count; j )

{ if (ADJACENCYLIST[destanation][j].Vertex2 == source)

ADJACENCYLIST[destanation].REMOVEAT(j);

}

} return true;

} return false;

} public override bool HASEDGE(int vertex1, int vertex2)

{ int source;

int destanation;

if (dictionary.TRYGETVALUE(vertex1, out source) && dictionary.TRYGETVALUE(vertex2, out destanation))

{ if (source > GETVERTICESCOUNT() - 1 || destanation > GETVERTICESCOUNT() - 1 || source < 0 || destanation < 0 || source == destanation) return false;

for (int i = 0; i < ADJACENCYLIST[source].Count; i ) if (ADJACENCYLIST[source][i].Vertex2 == destanation) return true;

} return false;

} public override W GETEDGEWEIGHT(int vertex1, int vertex2)

{ if (vertex1 == vertex2 || !dictionary.CONTAINSKEY(vertex1) || !dictionary.CONTAINSKEY(vertex2)) throw new ARGUMENTEXCEPTION();

if (!HASEDGE(vertex1, vertex2)) throw new ARGUMENTEXCEPTION();

int source = dictionary[vertex1];

int destanation = dictionary[vertex2];

if (source > GETVERTICESCOUNT() - 1 || destanation > GETVERTICESCOUNT() - 1 || source < 0 || destanation < 0) throw new ARGUMENTOUTOFRANGEEXCEPTION("this edge does not exist");

for (int i = 0; i < ADJACENCYLIST[source].Count; i ) if (ADJACENCYLIST[source][i].Vertex2 == destanation) return ADJACENCYLIST[source][i].Weight;

throw new ARGUMENTOUTOFRANGEEXCEPTION("Ребро не существует");

} public override bool SETEDGEWEIGHT(int vertex1, int vertex2, W weight)

{ if (vertex1 == vertex2 || !dictionary.CONTAINSKEY(vertex1) || !dictionary.CONTAINSKEY(vertex2)) return false;

if (!HASEDGE(vertex1, vertex2)) return false;

int source = dictionary[vertex1];

int destanation = dictionary[vertex2];

if (source > GETVERTICESCOUNT() - 1 || destanation > GETVERTICESCOUNT() - 1 || source < 0 || destanation < 0) return false;

for (int i = 0; i < ADJACENCYLIST[source].Count; i ) if (ADJACENCYLIST[source][i].Vertex2 == destanation)

{

ADJACENCYLIST[source][i].Weight = weight;

return true;

} return false;

} public override D GETEDGEDATA(int vertex1, int vertex2)

{ if (vertex1 == vertex2 || !dictionary.CONTAINSKEY(vertex1) || !dictionary.CONTAINSKEY(vertex2)) throw new ARGUMENTEXCEPTION();

if (!HASEDGE(vertex1, vertex2)) throw new ARGUMENTEXCEPTION();

int source = dictionary[vertex1];

int destanation = dictionary[vertex2];

if (source > GETVERTICESCOUNT() - 1 || destanation > GETVERTICESCOUNT() - 1 || source < 0 || destanation < 0) throw new ARGUMENTOUTOFRANGEEXCEPTION("this edge does not exist");

for (int i = 0; i < ADJACENCYLIST[source].Count; i ) if (ADJACENCYLIST[source][i].Vertex2 == destanation) return ADJACENCYLIST[source][i].Data;

throw new ARGUMENTOUTOFRANGEEXCEPTION("Ребро не существует");

} public override bool SETEDGEDATA(int vertex1, int vertex2, D data)

{ if (vertex1 == vertex2 || !dictionary.CONTAINSKEY(vertex1) || !dictionary.CONTAINSKEY(vertex2)) return false;

if (!HASEDGE(vertex1, vertex2)) return false;

int source = dictionary[vertex1];

int destanation = dictionary[vertex2];

if (source > GETVERTICESCOUNT() - 1 || destanation > GETVERTICESCOUNT() - 1 || source < 0 || destanation < 0) return false;

for (int i = 0; i < ADJACENCYLIST[source].Count; i ) if (ADJACENCYLIST[source][i].Vertex2 == destanation)

{

ADJACENCYLIST[source][i].Data = data;

return true;

} return false;

} public Object Clone()

{ return new LGRAPH(GETVERTICESCOUNT(), GETEDGESCOUNT(), ADJACENCYLIST, ISDIRECTED());

} public override Graph TOLISTGRAPH()

{ return this;

} public override Graph TOMATRIXGRAPH()

{

MGRAPH MGRAPH = new MGRAPH(GETVERTICESCOUNT(), ISDIRECTED());

foreach (List> LISTOFEDGES in ADJACENCYLIST) foreach (Edge edge in LISTOFEDGES)

{

MGRAPH.ADDEDGE(edge);

}

MGRAPH.dictionary = dictionary;

return MGRAPH;

} public override List GETNEIGHBORS(int VERTEXNUMBER)

{ int CURRENTVERTEXNEIGHBOR = 0;

List result = new List();

int min = 100;

if (ADJACENCYLIST[VERTEXNUMBER].Count != 0)

{ foreach (Edge edge in ADJACENCYLIST[VERTEXNUMBER])

{ if (edge.Vertex2 = CURRENTVERTEXNEIGHBOR)

{ min = edge.Vertex2;

}

} result.Add(min);

CURRENTVERTEXNEIGHBOR = min;

} while (true)

{ int old = CURRENTVERTEXNEIGHBOR;

min = 100;

foreach (Edge edge in ADJACENCYLIST[VERTEXNUMBER])

{ if (edge.Vertex2 old)

{ min = edge.Vertex2;

}

} if (min == 100)

{

CURRENTVERTEXNEIGHBOR = 0;

return result;

}

CURRENTVERTEXNEIGHBOR = min;

result.Add(CURRENTVERTEXNEIGHBOR);

}

} public class VERTEXITERATOR : Iterator

{ public VERTEXITERATOR(Graph GRAPHC)

: base(GRAPHC)

{

} public override void begin()

{ if (graph.GETVERTICESCOUNT() == 0)

{

CURRENTPOSITION = null;

return;

} int min = 100;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Key < min) min = entry.Key;

}

CURRENTPOSITION = new Edge(min, 100);

} public override void end()

{ if (graph.GETVERTICESCOUNT() == 0)

{

CURRENTPOSITION = null;

return;

} int max = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Key > max) max = entry.Key;

}

CURRENTPOSITION = new Edge(max, 100);

} public override void next()

{ if (CURRENTPOSITION == null)

{ return;

} int min = 100;

int old = CURRENTPOSITION.Vertex1;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Key old) min = entry.Key;

} if (min != 100)

{

CURRENTPOSITION = new Edge(min, 100);

} else

{

CURRENTPOSITION = null;

}

} public override Edge state()

{ return CURRENTPOSITION;

}

} public class EDGEITERATOR : Iterator

{ int LASTI = 0;

int LASTJ = 0;

int EDGESCOUNT = 0;

public EDGEITERATOR(Graph GRAPHC)

: base(GRAPHC)

{

} public override void begin()

{ if (graph.GETVERTICESCOUNT() == 0)

{

CURRENTPOSITION = null;

return;

} if (graph.ISDIRECTED())

{ int i = 0;

int j = 0;

bool ISEXIST = false;

while (!ISEXIST)

{ if (i == ((LGRAPH)graph).ADJACENCYLIST.Count)

{

CURRENTPOSITION = null;

return;

} if (((LGRAPH)graph).ADJACENCYLIST[i].Count == 0) i ;

else

{

ISEXIST = true;

}

} int source = ((LGRAPH)graph).ADJACENCYLIST[i][j].Vertex1;

int destanation = ((LGRAPH)graph).ADJACENCYLIST[i][j].Vertex2;

int SOURCEVERTEX = 0;

int DESTANATIONVERTEX = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == source) SOURCEVERTEX = entry.Key;

if (entry.Value == destanation) DESTANATIONVERTEX = entry.Key;

}

CURRENTPOSITION = new Edge(SOURCEVERTEX, DESTANATIONVERTEX, graph.GETEDGEWEIGHT(SOURCEVERTEX, DESTANATIONVERTEX), graph.GETEDGEDATA(SOURCEVERTEX, DESTANATIONVERTEX));

LASTI = i;

LASTJ = j;

EDGESCOUNT = 1;

} else

{ for (int i = 0; i <= MAXVERTICESCOUNT; i )

{ for (int j = 0; j <= MAXVERTICESCOUNT; j )

{ if (!graph.dictionary.CONTAINSKEY(i) || !graph.dictionary.CONTAINSKEY(j)) continue;

if (graph.HASEDGE(graph.dictionary[i], graph.dictionary[j]))

{ int SOURCEVERTEX = 0;

int DESTANATIONVERTEX = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == i) SOURCEVERTEX = entry.Key;

if (entry.Value == j) DESTANATIONVERTEX = entry.Key;

}

CURRENTPOSITION = new Edge(SOURCEVERTEX, DESTANATIONVERTEX, graph.GETEDGEWEIGHT(SOURCEVERTEX, DESTANATIONVERTEX), graph.GETEDGEDATA(SOURCEVERTEX, DESTANATIONVERTEX));

LASTI = i;

LASTJ = j;

EDGESCOUNT = 1;

return;

}

}

LASTJ = i;

}

}

} public override void end()

{ if (graph.GETVERTICESCOUNT() == 0)

{

CURRENTPOSITION = null;

return;

} if (graph.ISDIRECTED())

{ int i = ((LGRAPH)graph).ADJACENCYLIST.Count - 1;

bool ISEXIST = false;

while (!ISEXIST)

{ if (i == -1)

{

CURRENTPOSITION = null;

return;

} if (((LGRAPH)graph).ADJACENCYLIST[i].Count == 0) i-;

else

{

ISEXIST = true;

}

} int j = ((LGRAPH)graph).ADJACENCYLIST[i].Count - 1;

int source = ((LGRAPH)graph).ADJACENCYLIST[i][j].Vertex1;

int destanation = ((LGRAPH)graph).ADJACENCYLIST[i][j].Vertex2;

int SOURCEVERTEX = i;

int DESTANATIONVERTEX = j;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == source) SOURCEVERTEX = entry.Key;

if (entry.Value == destanation) DESTANATIONVERTEX = entry.Key;

}

CURRENTPOSITION = new Edge(SOURCEVERTEX, DESTANATIONVERTEX, graph.GETEDGEWEIGHT(SOURCEVERTEX, DESTANATIONVERTEX), graph.GETEDGEDATA(SOURCEVERTEX, DESTANATIONVERTEX));

EDGESCOUNT = graph.GETEDGESCOUNT();

} else

{ for (int i = MAXVERTICESCOUNT; i >= 0; i-) for (int j = MAXVERTICESCOUNT; j >= i; j-)

{ if (i == j || !graph.dictionary.CONTAINSKEY(i) || !graph.dictionary.CONTAINSKEY(j)) continue;

if (graph.HASEDGE(i, j))

{ int SOURCEVERTEX = 0;

int DESTANATIONVERTEX = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == i) SOURCEVERTEX = entry.Key;

if (entry.Value == j) DESTANATIONVERTEX = entry.Key;

}

CURRENTPOSITION = new Edge(SOURCEVERTEX, DESTANATIONVERTEX, graph.GETEDGEWEIGHT(SOURCEVERTEX, DESTANATIONVERTEX), graph.GETEDGEDATA(SOURCEVERTEX, DESTANATIONVERTEX));

LASTI = i;

LASTJ = j;

EDGESCOUNT = graph.GETEDGESCOUNT();

return;

}

}

}

} public override void next()

{ if (CURRENTPOSITION == null)

{ return;

} if (EDGESCOUNT == graph.GETEDGESCOUNT())

{

CURRENTPOSITION = null;

return;

} if (graph.ISDIRECTED())

{ int i = LASTI;

int j = LASTJ 1;

if (j == ((LGRAPH)graph).ADJACENCYLIST[i].Count)

{ j = 0;

i ;

} bool ISEXIST = false;

while (!ISEXIST)

{ if (i == ((LGRAPH)graph).ADJACENCYLIST.Count)

{

CURRENTPOSITION = null;

return;

} if (((LGRAPH)graph).ADJACENCYLIST[i].Count == 0) i ;

else

{

ISEXIST = true;

}

} if (i == ((LGRAPH)graph).ADJACENCYLIST.Count)

{

CURRENTPOSITION = null;

return;

} int source = ((LGRAPH)graph).ADJACENCYLIST[i][j].Vertex1;

int destanation = ((LGRAPH)graph).ADJACENCYLIST[i][j].Vertex2;

int SOURCEVERTEX = 0;

int DESTANATIONVERTEX = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == source) SOURCEVERTEX = entry.Key;

if (entry.Value == destanation) DESTANATIONVERTEX = entry.Key;

}

CURRENTPOSITION = new Edge(SOURCEVERTEX, DESTANATIONVERTEX, graph.GETEDGEWEIGHT(SOURCEVERTEX, DESTANATIONVERTEX), graph.GETEDGEDATA(SOURCEVERTEX, DESTANATIONVERTEX));

EDGESCOUNT ;

LASTI = i;

LASTJ = j;

return;

} else

{ for (int i = LASTI; i <= MAXVERTICESCOUNT; i )

{ for (int j = LASTJ 1; j <= MAXVERTICESCOUNT; j )

{ if (i == j || !graph.dictionary.CONTAINSKEY(i) || !graph.dictionary.CONTAINSKEY(j)) continue;

if (graph.HASEDGE(i, j))

{ int SOURCEVERTEX = i;

int DESTANATIONVERTEX = j;

CURRENTPOSITION = new Edge(SOURCEVERTEX, DESTANATIONVERTEX, graph.GETEDGEWEIGHT(SOURCEVERTEX, DESTANATIONVERTEX), graph.GETEDGEDATA(SOURCEVERTEX, DESTANATIONVERTEX));

LASTI = i;

LASTJ = j;

EDGESCOUNT ;

return;

}

}

LASTJ = i;

}

}

} public override Edge state()

{ return CURRENTPOSITION;

}

} public class INCOMINGEDGESITERATOR : Iterator

{ int LASTI = 0;

int VERTEXNUMB = -2;

public INCOMINGEDGESITERATOR(Graph GRAPHC, int VERTEXNUMBC)

: base(GRAPHC)

{

VERTEXNUMB = graph.dictionary[VERTEXNUMBC];

begin();

} public override void begin()

{ if (VERTEXNUMB == -2) return;

if (graph.GETVERTICESCOUNT() == 0)

{

CURRENTPOSITION = null;

return;

} for (int i = 0; i < graph.GETVERTICESCOUNT(); i )

{ int source = 0;

int destanation = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == i) source = entry.Key;

if (entry.Value == VERTEXNUMB) destanation = entry.Key;

} if (graph.HASEDGE(source, destanation))

{

CURRENTPOSITION = new Edge(source, destanation, graph.GETEDGEWEIGHT(source, destanation), graph.GETEDGEDATA(source, destanation));

LASTI = i;

return;

} if (i == graph.GETVERTICESCOUNT() - 1)

{

CURRENTPOSITION = null;

}

}

} public override void end()

{ if (graph.GETVERTICESCOUNT() == 0)

{

CURRENTPOSITION = null;

return;

} for (int i = graph.GETVERTICESCOUNT() - 1; i >= 0; i-)

{ int source = 0;

int destanation = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == i) source = entry.Key;

if (entry.Value == VERTEXNUMB) destanation = entry.Key;

} if (graph.HASEDGE(source, destanation))

{

CURRENTPOSITION = new Edge(source, destanation, graph.GETEDGEWEIGHT(source, destanation), graph.GETEDGEDATA(source, destanation));

LASTI = i;

return;

} if (i == 0)

{

CURRENTPOSITION = null;

}

}

} public override void next()

{ if (CURRENTPOSITION == null)

{ return;

} int i;

for (i = LASTI 1; i < graph.GETVERTICESCOUNT(); i )

{ int source = 0;

int destanation = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == i) source = entry.Key;

if (entry.Value == VERTEXNUMB) destanation = entry.Key;

} if (graph.HASEDGE(source, destanation))

{

CURRENTPOSITION = new Edge(source, destanation, graph.GETEDGEWEIGHT(source, destanation), graph.GETEDGEDATA(source, destanation));

LASTI = i;

return;

}

} if (i == graph.GETVERTICESCOUNT())

{

CURRENTPOSITION = null;

}

} public override Edge state()

{ return CURRENTPOSITION;

}

} public class OUTGOINGEDGESITERATOR : Iterator

{ int LASTI = 0;

int VERTEXNUMB = -2;

public OUTGOINGEDGESITERATOR(Graph GRAPHC, int VERTEXNUMBC)

: base(GRAPHC)

{

VERTEXNUMB = graph.dictionary[VERTEXNUMBC];

begin();

} public override void begin()

{ if (VERTEXNUMB == -2) return;

if (graph.GETVERTICESCOUNT() == 0)

{

CURRENTPOSITION = null;

return;

} if (((LGRAPH)graph).ADJACENCYLIST[VERTEXNUMB].Count == 0)

{

CURRENTPOSITION = null;

return;

} int i = 0;

int source = ((LGRAPH)graph).ADJACENCYLIST[VERTEXNUMB][i].Vertex1;

int destanation = ((LGRAPH)graph).ADJACENCYLIST[VERTEXNUMB][i].Vertex2;

bool SOURCEOK = false;

bool DESTANATIONOK = false;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == source && !SOURCEOK)

{ source = entry.Key;

SOURCEOK = true;

} if (entry.Value == destanation && !DESTANATIONOK)

{ destanation = entry.Key;

DESTANATIONOK = true;

}

}

CURRENTPOSITION = new Edge(source, destanation, graph.GETEDGEWEIGHT(source, destanation), graph.GETEDGEDATA(source, destanation));

LASTI = i;

} public override void end()

{ if (graph.GETVERTICESCOUNT() == 0)

{

CURRENTPOSITION = null;

return;

} if (((LGRAPH)graph).ADJACENCYLIST[VERTEXNUMB].Count == 0)

{

CURRENTPOSITION = null;

return;

} int i = ((LGRAPH)graph).ADJACENCYLIST[VERTEXNUMB].Count - 1;

int source = ((LGRAPH)graph).ADJACENCYLIST[VERTEXNUMB][i].Vertex1;

int destanation = ((LGRAPH)graph).ADJACENCYLIST[VERTEXNUMB][i].Vertex2;

bool SOURCEOK = false;

bool DESTANATIONOK = false;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == source && !SOURCEOK)

{ source = entry.Key;

SOURCEOK = true;

} if (entry.Value == destanation && !DESTANATIONOK)

{ destanation = entry.Key;

DESTANATIONOK = true;

}

}

CURRENTPOSITION = new Edge(source, destanation, graph.GETEDGEWEIGHT(source, destanation), graph.GETEDGEDATA(source, destanation));

LASTI = i;

} public override void next()

{ if (CURRENTPOSITION == null)

{ return;

} int i = LASTI 1;

if (i == ((LGRAPH)graph).ADJACENCYLIST[VERTEXNUMB].Count)

{

CURRENTPOSITION = null;

return;

} int source = ((LGRAPH)graph).ADJACENCYLIST[VERTEXNUMB][i].Vertex1;

int destanation = ((LGRAPH)graph).ADJACENCYLIST[VERTEXNUMB][i].Vertex2;

bool SOURCEOK = false;

bool DESTANATIONOK = false;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == source && !SOURCEOK)

{ source = entry.Key;

SOURCEOK = true;

} if (entry.Value == destanation && !DESTANATIONOK)

{ destanation = entry.Key;

DESTANATIONOK = true;

}

}

CURRENTPOSITION = new Edge(source, destanation, graph.GETEDGEWEIGHT(source, destanation), graph.GETEDGEDATA(source, destanation));

LASTI = i;

} public override Edge state()

{ return CURRENTPOSITION;

}

}

}

}

MGRAPH.cs using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace RGR

{ class MGRAPH : Graph, ICLONEABLE

{

Edge[,] ADJACENCYMATRIX;

public MGRAPH(int CVERTEX, bool CDIRECTED)

: base(CVERTEX, CDIRECTED)

{

ADJACENCYMATRIX = new Edge[CVERTEX * 2, CVERTEX * 2];

dictionary = new SORTEDDICTIONARY();

for (int i = 0; i < CVERTEX; i )

{ dictionary.Add(i, i);

}

} public MGRAPH(int CVERTEX, int CEDGE, bool CDIRECTED)

: base(CVERTEX, CDIRECTED)

{

ADJACENCYMATRIX = new Edge[CVERTEX * 2, CVERTEX * 2];

dictionary = new SORTEDDICTIONARY();

for (int i = 0; i < CVERTEX; i )

{ dictionary.Add(i, i);

}

Random rand = new Random();

for (int i = 0; i < CEDGE; )

{ int vertex1 = rand.Next(CVERTEX);

int vertex2 = rand.Next(CVERTEX);

bool EDGEINSERTED = ADDEDGE(vertex1, vertex2);

if (EDGEINSERTED) i ;

}

} public MGRAPH(int CVERTEX, int CEDGE, Edge[,] CADJACENCYMATRIX, bool CDIRECTED)

: base(CVERTEX, CEDGE, CDIRECTED)

{

ADJACENCYMATRIX = CADJACENCYMATRIX;

} public override bool GETTYPE()

{ return false;

} public override bool ADDVERTEX(int VERTEXNUMBER)

{ if (VERTEXNUMBER < 0) return false;

dictionary.Add(VERTEXNUMBER, GETVERTICESCOUNT());

int length = ADJACENCYMATRIX.GETLENGTH(0);

if (GETVERTICESCOUNT() == length) GROWARRAY();

INCREASEVERTICESCOUNT();

return true;

} private void GROWARRAY()

{ int NEWLENGTH;

int OLDLENGTH = ADJACENCYMATRIX.GETLENGTH(0);

if (OLDLENGTH == 0) NEWLENGTH = 2;

else NEWLENGTH = OLDLENGTH * 2;

Edge[,] NEWADJACENCYMATRIX = new Edge[NEWLENGTH, NEWLENGTH];

for (int i = 0; i < OLDLENGTH; i )

{ for (int j = 0; j < OLDLENGTH; j )

NEWADJACENCYMATRIX[i, j] = ADJACENCYMATRIX[i, j];

}

ADJACENCYMATRIX = NEWADJACENCYMATRIX;

} public override bool REMOVEVERTEX(int VERTEXNUMBER)

{ if (VERTEXNUMBER < 0) return false;

int DELETEINDEX = dictionary[VERTEXNUMBER];

int DELETEEDGESCOUNT = 0;

for (int i = 0; i < GETVERTICESCOUNT(); i )

{ if (ADJACENCYMATRIX[DELETEINDEX, i] != null) DELETEEDGESCOUNT ;

if (ADJACENCYMATRIX[i, DELETEINDEX] != null) DELETEEDGESCOUNT ;

}

DECREASEEDGESCOUNT(DELETEEDGESCOUNT);

DECREASEVERTICESCOUNT();

for (int i = DELETEINDEX; i < GETVERTICESCOUNT(); i ) for (int j = 0; j < GETVERTICESCOUNT() 1; j )

{

ADJACENCYMATRIX[i, j] = ADJACENCYMATRIX[i 1, j];

if (ADJACENCYMATRIX[i, j] != null)

{

ADJACENCYMATRIX[i, j].Vertex1 = i;

ADJACENCYMATRIX[i, j].Vertex2 = j;

}

} for (int i = 0; i < GETVERTICESCOUNT() 1; i ) for (int j = DELETEINDEX; j < GETVERTICESCOUNT(); j )

{

ADJACENCYMATRIX[i, j] = ADJACENCYMATRIX[i, j 1];

if (ADJACENCYMATRIX[i, j] != null)

{

ADJACENCYMATRIX[i, j].Vertex1 = i;

ADJACENCYMATRIX[i, j].Vertex2 = j;

}

} int CLEARINDEX = GETVERTICESCOUNT();

for (int i = 0; i < GETVERTICESCOUNT() ; i )

{

ADJACENCYMATRIX[CLEARINDEX, i] = null;

ADJACENCYMATRIX[i, CLEARINDEX] = null;

} dictionary.Remove(VERTEXNUMBER);

for (int key = 0; key <= MAXVERTICESCOUNT; key )

{ int value;

if (dictionary.TRYGETVALUE(key, out value))

{ if (value > VERTEXNUMBER)

{ dictionary.Remove(key);

dictionary.Add(key, value - 1);

}

}

} return true;

} public override bool ADDEDGE(int vertex1, int vertex2)

{ if (HASEDGE(vertex1, vertex2)) return false;

if (vertex1 == vertex2 || !dictionary.CONTAINSKEY(vertex1) || !dictionary.CONTAINSKEY(vertex2)) return false;

int source = dictionary[vertex1];

int destanation = dictionary[vertex2];

if (source < 0 || destanation < 0 || source == destanation) return false;

if (ADJACENCYMATRIX[source, destanation] != null) return false;

ADJACENCYMATRIX[source, destanation] = new Edge(source, destanation);

INCREASEEDGESCOUNT();

if (!ISDIRECTED())

{

ADJACENCYMATRIX[destanation, source] = new Edge(destanation, source);

} return true;

} public override bool ADDEDGE(Edge edge)

{ if (edge == null) return false;

ADJACENCYMATRIX[edge.Vertex1, edge.Vertex2] = edge;

INCREASEEDGESCOUNT();

if (!ISDIRECTED())

{

Edge REVERSEEDGE = new Edge(edge.Vertex2, edge.Vertex1, edge.Weight, edge.Data);

ADJACENCYMATRIX[edge.Vertex2, edge.Vertex1] = REVERSEEDGE;

} return true;

} public override bool REMOVEEDGE(int vertex1, int vertex2)

{ if (vertex1 == vertex2 || !dictionary.CONTAINSKEY(vertex1) || !dictionary.CONTAINSKEY(vertex2)) return false;

if (!HASEDGE(vertex1, vertex2)) return false;

int source = dictionary[vertex1];

int destanation = dictionary[vertex2];

if (source < 0 || destanation < 0 || source == destanation) return false;

if (ADJACENCYMATRIX[source, destanation] == null) return false;

ADJACENCYMATRIX[source, destanation] = null;

DECREASEEDGESCOUNT();

if (!ISDIRECTED())

{

ADJACENCYMATRIX[destanation, source] = null;

} return true;

} public override bool HASEDGE(int vertex1, int vertex2)

{ if (vertex1 == vertex2 || !dictionary.CONTAINSKEY(vertex1) || !dictionary.CONTAINSKEY(vertex2)) return false;

int source;

int destanation;

if (dictionary.TRYGETVALUE(vertex1, out source) && dictionary.TRYGETVALUE(vertex2, out destanation))

{ if (source < 0 || destanation < 0 || source == destanation) return false;

if(ADJACENCYMATRIX[source, destanation] != null) return true;

} return false;

} public override W GETEDGEWEIGHT(int vertex1, int vertex2)

{ if (vertex1 == vertex2 || !dictionary.CONTAINSKEY(vertex1) || !dictionary.CONTAINSKEY(vertex2)) throw new ARGUMENTEXCEPTION();

if (!HASEDGE(vertex1, vertex2)) throw new ARGUMENTEXCEPTION();

int source = dictionary[vertex1];

int destanation = dictionary[vertex2];

if (source < 0 || destanation < 0 || source == destanation) new ARGUMENTOUTOFRANGEEXCEPTION("Ребро не существует");

if (ADJACENCYMATRIX[source, destanation] != null) return ADJACENCYMATRIX[source, destanation].Weight;

throw new ARGUMENTOUTOFRANGEEXCEPTION("Ребро не существует");

} public override bool SETEDGEWEIGHT(int vertex1, int vertex2, W weight)

{ if (vertex1 == vertex2 || !dictionary.CONTAINSKEY(vertex1) || !dictionary.CONTAINSKEY(vertex2)) return false;

if (!HASEDGE(vertex1, vertex2)) return false;

int source = dictionary[vertex1];

int destanation = dictionary[vertex2];

if (source > GETVERTICESCOUNT() - 1 || destanation > GETVERTICESCOUNT() - 1 || source < 0 || destanation < 0) return false;

if (ADJACENCYMATRIX[source, destanation] != null)

{

ADJACENCYMATRIX[source, destanation].Weight = weight;

if (!ISDIRECTED()) ADJACENCYMATRIX[destanation, source].Weight = weight;

return true;

} return false;

} public override D GETEDGEDATA(int vertex1, int vertex2)

{ if (vertex1 == vertex2 || !dictionary.CONTAINSKEY(vertex1) || !dictionary.CONTAINSKEY(vertex2)) throw new ARGUMENTEXCEPTION();

if (!HASEDGE(vertex1, vertex2)) throw new ARGUMENTEXCEPTION();

int source = dictionary[vertex1];

int destanation = dictionary[vertex2];

if (source > GETVERTICESCOUNT() - 1 || destanation > GETVERTICESCOUNT() - 1 || source < 0 || destanation < 0) throw new ARGUMENTOUTOFRANGEEXCEPTION("this edge does not exist");

if (ADJACENCYMATRIX[source, destanation] != null) return ADJACENCYMATRIX[source, destanation].Data;

throw new ARGUMENTOUTOFRANGEEXCEPTION("Ребро не существует");

} public override bool SETEDGEDATA(int vertex1, int vertex2, D data)

{ if (vertex1 == vertex2 || !dictionary.CONTAINSKEY(vertex1) || !dictionary.CONTAINSKEY(vertex2)) return false;

if (!HASEDGE(vertex1, vertex2)) return false;

int source = dictionary[vertex1];

int destanation = dictionary[vertex2];

if (source > GETVERTICESCOUNT() - 1 || destanation > GETVERTICESCOUNT() - 1 || source < 0 || destanation < 0) return false;

if (ADJACENCYMATRIX[source, destanation] != null)

{

ADJACENCYMATRIX[source, destanation].Data = data;

if (!ISDIRECTED()) ADJACENCYMATRIX[destanation, source].Data = data;

return true;

} return false;

} public Object Clone()

{ return new MGRAPH(GETVERTICESCOUNT(), GETEDGESCOUNT(), ADJACENCYMATRIX, ISDIRECTED());

} public override Graph TOMATRIXGRAPH()

{ return this;

} public override Graph TOLISTGRAPH()

{

LGRAPH LGRAPH = new LGRAPH(GETVERTICESCOUNT(), ISDIRECTED());

if (ISDIRECTED())

{ for (int i = 0; i < GETVERTICESCOUNT(); i ) for (int j = 0; j < GETVERTICESCOUNT(); j )

{ if (ADJACENCYMATRIX[i, j] != null)

{

LGRAPH.ADDEDGE(ADJACENCYMATRIX[i, j]);

}

}

} else

{ for (int i = 0; i < GETVERTICESCOUNT(); i ) for (int j = i 1; j < GETVERTICESCOUNT(); j )

{ if (ADJACENCYMATRIX[i, j] != null)

{

LGRAPH.ADDEDGE(ADJACENCYMATRIX[i, j]);

}

}

}

LGRAPH.dictionary = dictionary;

return LGRAPH;

} public override List GETNEIGHBORS(int VERTEXNUMBER)

{ int CURRENTVERTEXNEIGHBOR = 0;

List result = new List();

while (true)

{ while (ADJACENCYMATRIX[VERTEXNUMBER, CURRENTVERTEXNEIGHBOR] == null)

{ if (CURRENTVERTEXNEIGHBOR == GETVERTICESCOUNT())

{ return result;

}

CURRENTVERTEXNEIGHBOR ;

} result.Add(ADJACENCYMATRIX[VERTEXNUMBER, CURRENTVERTEXNEIGHBOR ].Vertex2);

}

} public class VERTEXITERATOR : Iterator

{ public VERTEXITERATOR(Graph GRAPHC)

: base(GRAPHC)

{

} public override void begin()

{ if (graph.GETVERTICESCOUNT() == 0)

{

CURRENTPOSITION = null;

return;

} int min = 100;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Key < min) min = entry.Key;

}

CURRENTPOSITION = new Edge(min, 100);

} public override void end()

{ if (graph.GETVERTICESCOUNT() == 0)

{

CURRENTPOSITION = null;

return;

} int max = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Key > max) max = entry.Key;

}

CURRENTPOSITION = new Edge(max, 100);

} public override void next()

{ if (CURRENTPOSITION == null)

{ return;

} int min = 100;

int old = CURRENTPOSITION.Vertex1;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Key old) min = entry.Key;

} if (min != 100)

{

CURRENTPOSITION = new Edge(min, 100);

} else

{

CURRENTPOSITION = null;

}

} public override Edge state()

{ return CURRENTPOSITION;

}

} public class EDGEITERATOR : Iterator

{ int LASTI = 0;

int LASTJ = 0;

int EDGESCOUNT = 0;

public EDGEITERATOR(Graph GRAPHC)

: base(GRAPHC)

{

} public override void begin()

{ if (graph.GETVERTICESCOUNT() == 0)

{

CURRENTPOSITION = null;

return;

} if (graph.ISDIRECTED())

{ for (int i = 0; i < graph.GETVERTICESCOUNT(); i ) for (int j = 0; j < graph.GETVERTICESCOUNT(); j )

{ int source = 0;

int destanation = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == i) source = entry.Key;

if (entry.Value == j) destanation = entry.Key;

} if (graph.HASEDGE(source, destanation))

{

CURRENTPOSITION = new Edge(source, destanation, graph.GETEDGEWEIGHT(source, destanation), graph.GETEDGEDATA(source, destanation));

LASTI = i;

LASTJ = j;

EDGESCOUNT = 1;

return;

}

}

} else

{ for (int i = 0; i < graph.GETVERTICESCOUNT(); i ) for (int j = i; j < graph.GETVERTICESCOUNT(); j )

{ int source = 0;

int destanation = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == i) source = entry.Key;

if (entry.Value == j) destanation = entry.Key;

} if (graph.HASEDGE(source, destanation))

{

CURRENTPOSITION = new Edge(source, destanation, graph.GETEDGEWEIGHT(source, destanation), graph.GETEDGEDATA(source, destanation));

LASTI = i;

LASTJ = j;

EDGESCOUNT = 1;

return;

}

}

}

} public override void end()

{ if (graph.GETVERTICESCOUNT() == 0)

{

CURRENTPOSITION = null;

return;

} if (graph.ISDIRECTED())

{ for (int i = graph.GETVERTICESCOUNT() - 1; i >= 0; i-) for (int j = graph.GETVERTICESCOUNT() - 1; j >= 0; j-)

{ int source = 0;

int destanation = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == i) source = entry.Key;

if (entry.Value == j) destanation = entry.Key;

} if (graph.HASEDGE(source, destanation))

{

CURRENTPOSITION = new Edge(source, destanation, graph.GETEDGEWEIGHT(source, destanation), graph.GETEDGEDATA(source, destanation));

LASTI = i;

LASTJ = j;

EDGESCOUNT = 1;

return;

}

}

} else

{ for (int i = graph.GETVERTICESCOUNT() - 1; i >= 0; i-) for (int j = graph.GETVERTICESCOUNT() - 1; j >= i; j-)

{ int source = 0;

int destanation = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == i) source = entry.Key;

if (entry.Value == j) destanation = entry.Key;

} if (graph.HASEDGE(source, destanation))

{

CURRENTPOSITION = new Edge(source, destanation, graph.GETEDGEWEIGHT(source, destanation), graph.GETEDGEDATA(source, destanation));

LASTI = i;

LASTJ = j;

EDGESCOUNT = 1;

return;

}

}

}

} public override void next()

{ if (CURRENTPOSITION == null)

{ return;

} if (EDGESCOUNT == graph.GETEDGESCOUNT())

{

CURRENTPOSITION = null;

return;

} if (graph.ISDIRECTED())

{ for (int i = LASTI; i < graph.GETVERTICESCOUNT(); i )

{ for (int j = LASTJ 1; j < graph.GETVERTICESCOUNT(); j )

{ int source = 0;

int destanation = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == i) source = entry.Key;

if (entry.Value == j) destanation = entry.Key;

} if (graph.HASEDGE(source, destanation))

{

CURRENTPOSITION = new Edge(source, destanation, graph.GETEDGEWEIGHT(source, destanation), graph.GETEDGEDATA(source, destanation));

LASTI = i;

LASTJ = j;

EDGESCOUNT ;

return;

}

}

LASTJ = -1;

}

} else

{ for (int i = LASTI; i < graph.GETVERTICESCOUNT(); i )

{ for (int j = LASTJ 1; j < graph.GETVERTICESCOUNT(); j )

{ int source = 0;

int destanation = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == i) source = entry.Key;

if (entry.Value == j) destanation = entry.Key;

} if (graph.HASEDGE(source, destanation))

{

CURRENTPOSITION = new Edge(source, destanation, graph.GETEDGEWEIGHT(source, destanation), graph.GETEDGEDATA(source, destanation));

LASTI = i;

LASTJ = j;

EDGESCOUNT = 1;

return;

}

}

LASTJ = i;

}

}

} public override Edge state()

{ return CURRENTPOSITION;

}

} public class INCOMINGEDGESITERATOR : Iterator

{ int LASTI = 0;

int VERTEXNUMB = -2;

public INCOMINGEDGESITERATOR(Graph GRAPHC, int VERTEXNUMBC)

: base(GRAPHC)

{

VERTEXNUMB = graph.dictionary[VERTEXNUMBC];

begin();

} public override void begin()

{ if (VERTEXNUMB == -2) return;

if (graph.GETVERTICESCOUNT() == 0)

{

CURRENTPOSITION = null;

return;

} int i;

for (i = 0; i < graph.GETVERTICESCOUNT(); i )

{ int source = 0;

int destanation = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == i) source = entry.Key;

if (entry.Value == VERTEXNUMB) destanation = entry.Key;

} if (graph.HASEDGE(source, destanation))

{

CURRENTPOSITION = new Edge(source, destanation, graph.GETEDGEWEIGHT(source, destanation), graph.GETEDGEDATA(source, destanation));

LASTI = i;

return;

}

} if (i == graph.GETVERTICESCOUNT())

{

CURRENTPOSITION = null;

}

} public override void end()

{ if (graph.GETVERTICESCOUNT() == 0)

{

CURRENTPOSITION = null;

return;

} int i;

for (i = graph.GETVERTICESCOUNT() - 1; i >= 0; i-)

{ int source = 0;

int destanation = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == i) source = entry.Key;

if (entry.Value == VERTEXNUMB) destanation = entry.Key;

} if (graph.HASEDGE(source, destanation))

{

CURRENTPOSITION = new Edge(source, destanation, graph.GETEDGEWEIGHT(source, destanation), graph.GETEDGEDATA(source, destanation));

LASTI = i;

return;

}

} if (i == 0)

{

CURRENTPOSITION = null;

}

} public override void next()

{ if (CURRENTPOSITION == null)

{ return;

} int i;

for (i = LASTI 1; i < graph.GETVERTICESCOUNT(); i )

{ int source = 0;

int destanation = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == i) source = entry.Key;

if (entry.Value == VERTEXNUMB) destanation = entry.Key;

} if (graph.HASEDGE(source, destanation))

{

CURRENTPOSITION = new Edge(source, destanation, graph.GETEDGEWEIGHT(source, destanation), graph.GETEDGEDATA(source, destanation));

LASTI = i;

return;

}

} if (i == graph.GETVERTICESCOUNT())

{

CURRENTPOSITION = null;

}

} public override Edge state()

{ return CURRENTPOSITION;

}

} public class OUTGOINGEDGESITERATOR : Iterator

{ int LASTI = 0;

int VERTEXNUMB;

public OUTGOINGEDGESITERATOR(Graph GRAPHC, int VERTEXNUMBC)

: base(GRAPHC)

{

VERTEXNUMB = graph.dictionary[VERTEXNUMBC];

begin();

} public override void begin()

{ if (graph.GETVERTICESCOUNT() == 0)

{

CURRENTPOSITION = null;

return;

} int i;

for (i = 0; i < graph.GETVERTICESCOUNT(); i )

{ int source = 0;

int destanation = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == VERTEXNUMB) source = entry.Key;

if (entry.Value == i) destanation = entry.Key;

} if (graph.HASEDGE(source, destanation))

{

CURRENTPOSITION = new Edge(source, destanation, graph.GETEDGEWEIGHT(source, destanation), graph.GETEDGEDATA(source, destanation));

LASTI = i;

return;

}

} if (i == graph.GETVERTICESCOUNT())

{

CURRENTPOSITION = null;

}

} public override void end()

{ if (graph.GETVERTICESCOUNT() == 0)

{

CURRENTPOSITION = null;

return;

} int i;

for (i = graph.GETVERTICESCOUNT() - 1; i >= 0; i-)

{ int source = 0;

int destanation = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == VERTEXNUMB) source = entry.Key;

if (entry.Value == i) destanation = entry.Key;

} if (graph.HASEDGE(source, destanation))

{

CURRENTPOSITION = new Edge(source, destanation, graph.GETEDGEWEIGHT(source, destanation), graph.GETEDGEDATA(source, destanation));

LASTI = i;

return;

} if (i == 0)

{

CURRENTPOSITION = null;

}

}

} public override void next()

{ if (CURRENTPOSITION == null)

{ return;

} int i;

for (i = LASTI 1; i < graph.GETVERTICESCOUNT(); i )

{ int source = 0;

int destanation = 0;

foreach (KEYVALUEPAIR entry in graph.dictionary)

{ if (entry.Value == VERTEXNUMB) source = entry.Key;

if (entry.Value == i) destanation = entry.Key;

} if (graph.HASEDGE(source, destanation))

{

CURRENTPOSITION = new Edge(source, destanation, graph.GETEDGEWEIGHT(source, destanation), graph.GETEDGEDATA(source, destanation));

LASTI = i;

return;

}

} if (i == graph.GETVERTICESCOUNT())

{

CURRENTPOSITION = null;

}

} public override Edge state()

{ return CURRENTPOSITION;

}

}

}

}

Размещено на
Заказать написание новой работы



Дисциплины научных работ



Хотите, перезвоним вам?