gt;
# include lt; queue gt;
# include lt; iostream gt; namespace std; main ()
{ lt; vector lt; int gt; gt; g;// Граф
const int n=4;// Число вершінs=0;// Стартова вершина (вершини всюди нумеруються з нуля)
//читання графаAdj [n] [n]={
{0,1,0,0},
{1,0,1,0},
{0,1,0,1},
{0,0,1,0}}; (int i=0; i lt; n; i ++)
{. push_back (vector lt; int gt; ()); (int j=0; j lt; n; j ++)
{[i]. push_back (0); [i] [j]=Adj [i] [j];
}
} lt; int gt; q ;. push (s); lt; bool gt; used (n); lt; int gt; d (n), p (n); [s]=true; [s]=- 1; (! q. empty ())
{v=q. front (); (size_t i=0; i lt; g [v]. size (); ++ i)
{(! used [i] amp; amp; g [v] [i])
{[i]=true ;. push (i);
d [i]=d [v] + 1;// Відстань;
p [i]=v;// Parent;
}
}. pop ();
} (int i=0; i lt; n; i ++) lt; lt; d [i] lt; lt; raquo ;; lt; lt; endl; (int i=0; i lt; n; i ++) lt; lt; p [i] lt; lt; raquo ;; lt; lt; endl;
system ( pause ); 0;
}
Pascal
const=1000; i, j, n, consp, cur, st, fin: integer; way, q: array [1. l] of integer ;: array [1. l] of integer ;: array [1.100] of array [1.100] of integer ;: array [1. l] of integer;, e: integer; add (var i: integer); i lt; l then i:=i + 1 else i:=1 ;; i:=1 to l do [i]:=0; [i]:=0 ;; (n); i:=1 to n doj:= 1 to n do (m [i, j]); (st, fin) ;:=1 ;:=b + 1; [b]:=st; (cur lt; gt; fin) do:=q [b] ; (b); [cur]:=1; (b lt; gt; e) and (cur lt; gt; fin) theni:=1 to n do ((m [i, cur]=1) and (was [ i]=0)) then
begin
add (e); [e]:=i; [i]:=cur ;;;;:=1; [1]:=fin; (cur lt; gt; st) do [i]:=cur; (i) ;:=pred [cur] ;; i:=1 to i do (way [i]) ;;;
end.
Код Java (TM) 2 Platform Standard Edition 5.0
public class WideAlgo {static void main (String [] args) {theGraph=new Graph () ;. addVertex ( A );// 0. addVertex ( B );// 1. addVertex ( C );// 2. addVertex ( D );// 3. addVertex ( E );// 4. addEdge (0, 1);// AB. addEdge (1,2);// BC. addEdge (0,3);// AD. addEdge (3,4);// DE. out. print ( Відвідини: ) ;. bfs () ;. out. println ();
} class Queue {final int SIZE=20; int [] queArray; int front; int rear; Queue () {= new int [SIZE];=0;=- 1;
} void insert (int j)//put item at rear of queue
{(rear == SIZE - 1)=- 1; [++ rear]=j;
} int remove ()//take item from front of queue
{temp=queArray [front ++]; (front == SIZE)=0; temp;
} boolean isEmpty ()//true if queue is empty
{(rear + 1 == front || (front + SIZE - 1 == rear));
}
} class Vertex {char label;// Labelboolean wasVisited; Vertex (char l) {= l;=false;
}
} class Graph {final int MAX_VERTS=20; Vertex vertexList [];// List of verticesint adjMat [] [];// Adjacency matrixint nVerts;// Current number of verticesQueue theQueue; Graph () {= new Vertex [MAX_VERTS];
//adjacency matrix=new int [MAX_VERTS] [MAX_VERTS];=0; (int j=0; j lt; MAX_VERTS; j ++)
//set adjacency (int k=0; k lt; MAX_VERTS; k ++)
//matrix to 0 [j] [k]=0;=new Queue ();
} void addVertex (char l) {[nVerts ++]=new Vertex (l);
} void addEdge (int start, int end) {[start] [end]=1; [end] [start]=1;
} void displayVertex (int v) {. out. print (vertexList [v]. label);
}
//breadth-first searchvoid bfs ()
{// begin at vertex 0 [0]. wasVisited=true;// Mark it (0);// Display it. insert (0);// Insert at tailv2; (! TheQueue. IsEmpty ())//until queue empty,
{v1=theQueue. remove ();// Remove vertex at head
//until it has no unvisited neighbors ((v2=getAdjUnvisitedVertex (v1))!=- 1) {// get one, [v2]. wasVisited=true;// Mark it (v2);// Display it. insert (v2);// Insert it
}
}
//queue is empty, so we're done (int j=0; j lt; nVerts; j ++)
//reset flags [j]. wasVisited=false;