// The shortest path (path with the least number of edges) from one node to another in an undirected graph. cpp: Defines the entry point for the console application.
//
# Include "stdafx. h"
# Include <iostream>
# Deprecision MAX 100
# Define MAXQ 50
Using namespace std;
Struct edgeNode
{
Int no; // edge serial number
Char info; // edge name
Struct edgeNode * next; // next
};
Struct vexNode
{
Char info; // node name
Struct edgeNode * link; // endpoint connected to it
};
// Store node Information
VexNode adjlist [MAX];
// Cyclic queue
Int queue [MAXQ];
// Access flag
Bool visited [MAX];
// Store the path from the specified point to each point
Int parent [MAX];
// Create an adjacent table Storage and return the number of nodes
Int createGraph (vexNode * adjlist)
{
Int n, e;
Cout <"Enter the number of nodes :";
Cin> n;
Cout <"Enter the number of sides :";
Cin> e;
Int I;
For (I = 1; I <= n; I ++)
{
Cout <"Enter the node" <I <"name :";
Cin> adjlist [I]. info;
Adjlist [I]. link = NULL;
}
EdgeNode * p1, * p2;
Int v1, v2;
For (I = 1; I <= e; I ++)
{
Cout <"enter the second-end node number of Edge" <I <:";
Cin> v1> v2;
P1 = (edgeNode *) malloc (sizeof (edgeNode ));
P2 = (edgeNode *) malloc (sizeof (edgeNode ));
P1-> no = v1;
P1-> info = adjlist [v1]. info;
P1-> next = adjlist [v2]. link;
Adjlist [v2]. link = p1;
P2-> no = v2;
P2-> info = adjlist [v2]. info;
P2-> next = adjlist [v1]. link;
Adjlist [v1]. link = p2;
}
Return n;
}
// Returns the start point of the undirected graph with the extended search priority.
Int BFS (vexNode * adjlist, int * queue, bool * visited, int * parent)
{
Int front, rear, v1;
Cout <"Enter the serial number from which to start searching :";
Cin> v1;
Front = 0;
Rear = 1;
Queue [rear] = v1;
Int I;
// Clear the access flag
For (I = 1; I <MAX; I ++)
Visited [I] = false;
Visited [v1] = true;
Cout <"breadth-first search order:" <endl;
Cout <"Node" <v1 <", name" <adjlist [v1]. info <endl;
Int vx;
EdgeNode * p;
While (front! = Rear)
{
Front = (front + 1) % MAXQ;
Vx = queue [front];
P = adjlist [vx]. link;
While (p! = NULL)
{
If (! Visited [p-> no])
{
Visited [p-> no] = true;
Cout <"Node" <p-> no <", name" <adjlist [p-> no]. info <endl;
Rear = (rear + 1) % MAXQ;
Queue [rear] = p-> no;
Parent [p-> no] = vx;
}
P = p-> next;
}
}
Return v1;
}
// Print the path with the minimum number of edges from the start point to the target node (Shortest Path)
// V is the target node
Void print_line (vexNode * adjlist, int * parent, int v)
{
Int j = v;
If (parent [j]! = 0)
Print_line (adjlist, parent, parent [j]);
Cout <adjlist [j]. info <"";
}
Int _ tmain (int argc, _ TCHAR * argv [])
{
Int cases;
Cout <"Enter the number of cases :";
Cin> cases;
While (cases --)
{
// Create an adjacent table
Int n = createGraph (adjlist );
// Search for breadth first
Int v1 = BFS (adjlist, queue, visited, parent );
// The Start Node does not have a precursor Node
Parent [v1] = 0;
Int v;
Cout <"Enter the target node :";
Cin> v;
// Print the path with the minimum number of edges from the start point to the target node (Shortest Path)
Cout <"Start Node" <adjlist [v1]. info <"to" <adjlist [v]. info <"the Shortest Path of the node is:" <endl;
Print_line (adjlist, parent, v );
Cout <endl;
}
System ("pause ");
Return 0;
}
------------------------------------------------ Program test ------------------------------------------------
Enter the number of cases: 1
Enter the number of nodes: 8
Enter the number of edges: 10
Enter the name of Node 1: r
Enter Node 2 Name: s
Enter the name of Node 3: t
Enter node 4 name: u
Enter node 5 Name: v
Enter the name of node 6: w
Enter the name of node 7: x
Enter node 8 name: y
Enter the node number at the second end of edge 1: 1 2
Enter the node number at the second end of edge 2: 1 3
Enter the node number at the second end of edge 3: 2 4
Enter the node number at the second end of edge 4: 3 5
Enter the node number at the second end of edge 5: 3 6
Enter the node number at the second end of edge 6: 5 6
Enter the node number at the second end of edge 7: 5 8
Enter the node number at the second end of edge 8: 6 7
Enter the node number at the second end of edge 9: 6 8
Enter the node number at the second end of edge 10: 7 8
Enter the vertex of the serial number to start searching: 2
The priority search order for breadth is:
Node 2, name s
Node 4, name u
Node 1, name r
Node 3, name t
Node 6, name w
Node 5, name v
Node 8, Name y
Node 7, name x
Enter the target node: 6
The shortest path from the Start Node s to the w node is:
S r t w
Press any key to continue...
Author heyongluoyao8