Código fuente experimental del "gráfico" de estructura de datos que se necesita con urgencia
//////////////// / /////////////////////////////////////
/Dijkstra fuente única más corta ruta:
//////// ///////////////////////////////// //// ///////
#include
Usar espacio de nombres std;
#include
#include
const int MAXVALUE = 2147483647;
const int MAXVNUM = 100;
const int FUENTE = 1;
else
{
recuento de retornos;
}
}
visualización nula ( int dist [], int ruta[][MAXVNUM], int pos[], int n );//Declaración
void dijkstra( int A[][MAXVNUM], int n, int v )
{
bool s[n + 1]; // Indica si se ha encontrado la ruta más corta de v-s
int dist[n + 1 ]; // Indica v Valor de distancia entre i
int path[ MAXVNUM ][ MAXVNUM ] // Registro de ruta
int pos[n + 1];
int i;
for ( i = 1; i <= n; i++ )
{
s[i] = falso;
dist[i] = A[v][i] // o A[i][v]
ruta[i][1] = v;
pos[ i] = 1;
}
s[v] = true;
// Inicialización
para ( i = 1; i < n; i++ )
{
int objetivo;
objetivo = Find_shortest( s, dist, n ); camino más corto p>
afirmar (objetivo! = -1 );
s[destino] = verdadero;
ruta[destino][ ++pos[destino] ] = destino;
int cola [MAXVNUM];
int count,temp;
count = Find_accessible( A, s, target, n, queue ); // Número encontrado
if); ( -1 == recuento )
{
continuar;
}// La búsqueda falló
para ( int k = 1 ; k <= recuento; k++ )
{
temp = cola[ k - 1 ];
if ( dist[objetivo] + A[objetivo] [ temperatura]<dist[t
emp] )
{
dist[temp] = dist[destino] + A[destino][temp];
for ( int j = 1; j <= pos[objetivo]; j++ )
{
ruta[temp][j] = ruta[destino][j];
}
pos[temp] = pos[objetivo];
}
}
}
display( dist , ruta, pos, n ); // Mostrar resultados (rutas)
return;
}
void display( int dist[], int ruta[ ][MAXVNUM], int pos[], int n )
{
int i,j;
para ( i = 2; i <= n ; i++ )
{
cout << "Ruta " << FUENTE << " a " << i << " :" << endl;
para ( j = 1; j <= pos[i]; j++ )
{
si ( j != pos[i] )
{
cout << ruta[i][j] << "->";
}
else
{
cout << ruta[i][j] << endl {
int A[MAXVNUM][MAXVNUM];
int n; p>
p>
int e;
cin >> n >
ADJMATRIX( A, n, e);
/ /salida( A, n );
dijkstra( A, n, FUENTE );
sistema( "pausa" );
retorno 0;
}
////////////////////////////////// /////// //////////// ///Prim para árbol de expansión mínimo:
/////////////// //////// //////////////////////////////
#include
usando el espacio de nombres std;
#include
#include
const int MAXVALUE = 2147483647;
const int MAXVNUM = 100;
void ADJMATRIX( int A[][MAXVNUM], int n, int e )
{
int i;
int j;
int k;
int peso
para ( i = 1; i <
;= n; i++ )
{
para ( j = 1; j <= n; j++ )
{
A [i][j] = MAXVALUE;
}
}
}// Inicialización
para ( k = 0; k < e; k++ )
{
cin >> i >> j >> peso;
A[i][j] = peso; p>
A[j][i] = peso
}
retorno;
}
salida nula( int A[][MAXVNUM], int n )
{
int i;
int j;
cout << " La matriz adyacente de este gráfico es la siguiente: " << endl;
for ( i = 1; i <= n; i++ )
{
for ( j = 1; j <= n; j++ )
{
si ( j % n ! = 0 )
{
if ( A[i][j] != MAXVALUE )
{
cout << setw(3) << A[i][j] << ' ' ;
}
else
{
cout << setw(3) << ;
}
}
más
{
si ( A[i][j] != MAXVALUE )
{
cout << setw(3) << A[i][j] << endl;
}
else
{
cout << setw(3) << setw(3) <& lt;
}
}
}
retorno
}
anular MINSPRANT; ( int A[][MAXVNUM], int n )
{
int i,j,k,min;
int bajo costo[MAXVNUM],end [MAXVNUM];
bajo costo[1] = 0;
fin[1] = 1;
for ( i = 2; i <= n; i++ )
{
> bajo costo[i] = A[i][1];
fin[i] = 1;
}// Inicialización
for ( i = 1; i < n; i++ )
{
min = MAXVALUE;
para (j = 2; j <= n; j++) // j
= 1 cualquiera
{
if ( lowcost[j] > 0 && lowcost[j] < min )
{
k = j;
min = bajo costo[j];
}
}
bajo costo[k] = 0; p>
p>
// Genera uno de los bordes
cout << '(' << k << ',' << end[k] << ')' < & lt; endl;
para ( j = 1; j <= n; j++ )
{
si ( A[j][k] < bajo costo[j] )
{
bajo costo[j] = A[j][k];
fin[j] = k;
}
}// reajustar
}
return;
}
int main() p>
{
int A[ MAXVNUM ][ MAXVNUM ];
int n;
int e;<
cin >> n >> e;
ADJMATRIX( A, n, e );
MINSPRANT( A, n ); // Método Prim
salida ( A, n );
sistema( "pausa" );
devuelve 0;
}
///// ////// /////////////////////////////////////////// p>
// Amplitud -primera búsqueda
///////////////////////////////// ////////// ///////////
#include <iostream>
usando el espacio de nombres std;
#define vertype int
#define MAXVNUM 100
typedef struct edge
{
int adjvex;
int peso;
borde *siguiente;
}ELink;
typedef struct ver
{
vertype vertex; // cada vértice retransmite información
ELink *link;
}VLink;
// Variables globales
VLink G[MAXVNUM]; p>
A través de este apartado podemos ver la lista de las variables más importantes.
p>int n;
int e;
bool visitado[MAXVNUM];
Vector ELink*[MAXVNUM];
int Q[200];
int front = 0;
int rear = -1;
bool EMPTYQ( int Q[] )
{
si (delantero > trasero)
{
devuelve verdadero;
}
más
{
devuelve falso
}
}
void ADDQ( int Q[], int vértice )
{
Q[++trasero] = vértice;
retorno;
}
int DELQ( int Q[] )
{
return Q[front++];
}
void ADJLIST( VLink G[ ], int n, int e )
{
int i;
int j;
int k;
int peso;
for ( i = 0; i < n; i++ )
{
G[i].Vertex = i + 1 ;
G[i].link = NULL;
}//Inicialización
para ( k = 0; k < e; k++ )
{
cin >> i >> j >> peso;
ELink *temp = ( ELink* ) malloc ( tamaño de ( ELink ) );
temp->adjvex = j - 1;
temp->peso = peso;
temp->siguiente = NULL;
if ( NULL == G[ i - 1 ].link )
{
G[ i - 1 ].link = temp;
}
else
{
ELink *q = G[ i - 1 ].link;
while ( q->siguiente ! = NULL )
{
q = q->siguiente;
}
q->siguiente = temp;
}
}
}
retorno;
}
salida nula (VLink G [].int n )
{
int i;
ELink *temp;
cout << "La fase de este imagen La lista de vecinos se presenta de la siguiente manera: " << endl ;
for ( i = 0; i < n; i++ )
{
cout < < i < < " -> ";
temp = G[i].link;
mientras
( temp != NULL )
{
cout << temp->adjvex << '('
<< temp->peso << ') ' << " -> ";
temp = temp->siguiente;
}
cout << "NULL" << endl; p>
}
Retorno;
}
void DELVER( VLink G[], int &n, const vertype &item )
{
ELink *temp1;
ELink *temp2;
ELink *prior;
int k = -1;
int i;
for ( i = 0; i < n; i++ )
{
if ( elemento == G[ i].Vértice)
{
k = i;
romper;
}
}/ / Encuentra el vértice objetivo a eliminar
if ( k ! = -1 )
{
temp1 = G[k].link // Store; Puntero encontrado
para ( i = k + 1; i < n; i++ )
{
G[ i - 1 ].link = G[i ].link;
G[ i - 1 ].vertex = G[i - 1 ].1 ].vertex = G[i].vertex;
}// Mover elementos
n--; // Autodeducción para identificar el número real de vértices en el gráfico
while ( temp1 ! = NULL )
{
temp2 = temp1;
temp1 = temp1->next;
free(temp2);
} // Libera la memoria utilizada por el vértice objetivo
n--; // Autodeducción para identificar el número real de vértices en el gráfico
while ( temp1 !// Libera la memoria utilizada por el objetivo vértice
for ( i = 0; i < n; i++ )
{
temp1 = G[i].link;
mientras (temp1! = NULL )
{
si ( temp1->adjvex == k )
{
si ( G[i]. enlace == temp1 )
{
G[i].link = temp1->siguiente;
}
más p>
{
anterior->siguiente = temp1->siguiente;
}
ELink *temp3 = temp1;
temp1 = temp1->siguiente;
gratis( temp3);
}
más
{
si (temp1->adjvex > k )
{
temp1->adjvex--;
}
anterior = temp1;
temp1 = temp1->siguiente; p> p>
}
}
}
}
}
} p>
return;
}
anular VISITA( int v )
{
cout << G[ v] .vertex << ' ';
return;
}
int FIRSTADJ( VLink G[], int vértice )
{ p>
vector[vértice] = G[vértice].link;
if ( vector[vértice] != NULL )
{
return vector[ vértice] ->adjvex;
}
else
{
return -1;
}
}
int NEXTADJ( VLink G[], int vértice )
{
vector[vértice] = vector[vértice]- >siguiente ;
if ( vector[vértice] != NULL )
{
return vector[vértice]->adjvex;
}
else
{
return -1
}
}
void BFS( VLink G[], int vértice )
{
int w;
VISITA( vértice );
visitado[ vértice] = verdadero;
ADDQ( Q, vértice );
while ( !EMPTYQ (Q) )
{
vértice = DELQ(Q);
w = FIRSTADJ( G, vértice );
while ( w ! = -1 )
{
si (! visitado[w] )
{
VISITA(w);
visitado[w] = verdadero;
ADDQ( Q, w );
}
w = NEXTADJ( G, vértice );
w = FIRSTADJ( G, vértice );
mientras ( w ! vértice );
}
}
retorno;
}
void TRAVEL_BFS( VLink G[], bool visitado[], int n )
{
int i;
for ( i = 0 ; i < n; i++ )
{
visitado[i] = false;
}//Inicialización
cout << "El orden de generación de BFS es como sigue: " < <
endl;
for ( i = 0; i < n; i++ )
{
if ( !visited[i] )
{
BFS( G, i );
}
}
cout < < endl;<
return;
}
int main()
{
cin >> n >> e;
ADJLIST( G, n, e );
salida( G, n ); // Muestra la estructura de este gráfico
/*
DELVER ( G, n, 2 ); // Eliminar un vértice
salida( G, n ); // Generar la estructura de este gráfico nuevamente después de cambiar su estructura
DELVER( G, n, e );
salida( G, n ); // Genera la estructura de este gráfico nuevamente después de haberlo cambiado
*/
TRAVEL_BFS ( G, visitado, n );
sistema( "pausa" );
devuelve 0;
}
/ // //////////////////////// ///////////////////////// // p>
// Primera búsqueda en profundidad
///////////////////////////// ///// ///////////////////
#include <iostream><
usando el espacio de nombres std;
#define vertype int
#define MAXVNUM 100
typedef struct edge
{
int adjvex;
int peso;
borde *siguiente;
}ELink
typedef struct ver
{ p>
vertype vertex; // almacena información de cada vértice
ELink *link
}VLink;
// variable global
VLink G [MAXVNUM];
int n;
int e;
bool visitado[MAXVNUM];
Vector ELink*[ MAXVNUM];
void ADJLIST( VLink G[], int n, int e )
{
int i;
int j;
int k;
int peso;
for ( i = 0; i < n; i++ )
{
G[i].Vertex = i + 1;
G[i].link = NULL;
}//Inicialización
for ( k = 0 ; k < e; k++ )
{
cin >> i >> j >> peso;
ELink *temp = (EEnlace*
) malloc ( sizeof( ELink ) );
temp- >adjvex = j - 1;
temp- >peso = peso;
temp- >siguiente = NULL;
if ( NULL == G[ i - 1 ].link )
{
G[ i - 1 ].link = temp;
}
else
{
ELink *q = G[ i - 1 ].link;
mientras ( q->siguiente != NULL )
{
q = q->siguiente;
}
q- >siguiente = temporal;
}
}
}
retorno;
}
salida nula( VLink G[].int n )
{
int i;
ELink *temp;
cout << "La lista adyacente de este gráfico se presenta de la siguiente manera:" << endl
for ( i = 0; i < n; i++ )
{
cout << i << " -> ";
temp = G[i].link;
while ( temp != NULL )
{
cout << temp->adjvex << '('
<< temp->peso << ')' << " -> "; p >
temp = temp->siguiente;
}
cout << "NULL" << endl;
}
Retorno;
}
void DELVER( VLink G[], int &n, const vertype &item )
{
ELink * temp1;
ELink *temp2;
ELink *prior;
int k = -1;
int i; p >
for ( i = 0; i < n; i++ )
{
if ( elemento == G[i].vertex)
{
k = i;
break;
}
}// Encuentra el vértice objetivo que se eliminará
if ( k ! = -1 )
{
temp1 = G[k].link // Almacena el puntero encontrado
for; ( i = k + 1; i < n; i++ )
{
G[ i - 1 ].link = G[i].link;
G [ i - 1 ].vertex = G[i - 1 ].1 ].vertex = G[i].vertex;
}//Mover elementos
n- -; // Autodeducción para identificar la imagen
Número real de vértices
while ( temp1 ! = NULL )
{
temp2 = temp1;
temp1 = temp1->next ;
free(temp2);
} // Libera la memoria utilizada por el vértice objetivo
n-- // Autodeducción para identificar el vértices en el gráfico Cantidad real
while ( temp1 !// Libera la memoria utilizada por el vértice objetivo
for ( i = 0; i < n; i++ )
{ p>
temp1 = G[i].link;
mientras ( temp1 != NULL )
{
si ( temp1->adjvex = = k )
{
if ( G[i].link == temp1 )
{
G[i]. enlace = temp1->siguiente;
}
else
{
anterior->siguiente = temp1 ->siguiente; p>
}
ELink *temp3 = temp1;
temp1 = temp1->siguiente;
libre( temp3) ;
}
else
{
if ( temp1->adjvex > k )
{
temp1->adjvex--;
}
anterior = temp1;
temp1 = temp1->siguiente;
} p>
}
}
}
}
}
return;
}
VISITA nula( int v )
{
cout << G[ v].vertex << ' ';
return;
}
int FIRSTADJ( VLink G[], int vértice )
{
vector[vértice] = G[vértice].link;
if ( vector[vértice] ! = NULL )
{
devolver vector[vertex]->adjvex;
}
else
{
retorno -1;
}
}
int NEXTADJ( VLink G[], int vértice )
{
vector[vértice] = vector[vértice]->siguiente;
if ( vector[vértice] != NULL )
{
return vector[vertex]->adjvex;
}
else
{
return -1;
}
}
void DFS( VLink G[], int vértice )
{
VISITA( vértice );
visitado[vértice] = verdadero;
int w = FIRSTADJ( G, vértice );
while ( w ! = -1 )
{
if ( !visitó[w] )
{
DFS( G, w );
}
w = NEXTADJ( G, vértice );
}
Retorno
} p>
void TRAVEL_DFS( VLink G[], bool visitado[], int n )
{
int i;
for ( i = 0; i < n; i++ )
{
visitado[i] = false;
}/ / Inicialización
cout < < "El orden de generación de DFS es el siguiente:" << endl;
for ( i = 0; i < n; i++ )
{
if ( ! visitó[i] )
{
DFS( G, i );
}
}
cout << endl;
return;
}
int main()
{
Eliminar una salida de Vertex
( G, n
*/
TRAVEL_DFS( G, visitado, n ); sistema( "pausa" );
return 0;
}
////////////////// /// ////////////////////////////////
//////// ////// /////////////////////////////////////
#include
usando el espacio de nombres std;
#define vertype int
#define MAXVNUM 100
typedef struct edge p>
{ p>
int adjvex;
int peso;
borde *siguiente;
}ELink;
typedef struct ver
{
int intitle;
vertype vertex; // almacena información de cada vértice
ELink *enlace;
}VLink;
// Variable global
VLink G[MAXVNUM]
int
int e;
bool visitado[MAXVNUM];
ELink* vector[MAXVNUM] }
else
{
ELink *q = G[ i - 1 ].link;
while ( q->siguiente ! = NULO )
p>
{
q = q->siguiente;
}
q->siguiente = temp;
}
}
}
retorno;
}
salida nula (VLink G[].int n )
{
int i;
ELink *temp;
cout << "La lista adyacente de esta imagen se presenta como sigue: " << endl ;
for ( i = 0; i < n; i++ )
{
cout << i << '[' < < G[i].ingrado << ']'