Professional Documents
Culture Documents
Percobaan:
1. Graph Statis
A. Listing program
#include <stdio.h>
#include <stdlib.h>
#define N 16
#define M 9999
#define MAX 5
typedef struct{
int data[MAX];
int count;
} stack;
typedef struct{
int Q[N][N], P[N][N], R[N][N];
}typegraph;
typegraph graph;
stack tumpukan;
int jml_ordo;
int main()
{
int asal, tujuan, load;
typegraph *g = &graph;
printf("Graph - Warshall\n\n");
input(g);
tampilmatrikslama(g);
warshall(g);
tampilmatriksbaru(g);
printf("Masukkan asal : ");
P R A K T I K U M A L G O R I T M A D A N S T R U K T U R D A T A 2 | 16
Mei 2, 2021
scanf("%d", &asal);
printf("Masukkan tujuan : ");
scanf("%d", &tujuan);
load = ReadingRoute(asal - 1, tujuan - 1,
g);
printf("Beban : %d\n\n", load);
return 0;
}
void input(typegraph *g){
int i, j;
P R A K T I K U M A L G O R I T M A D A N S T R U K T U R D A T A 3 | 16
Mei 2, 2021
P R A K T I K U M A L G O R I T M A D A N S T R U K T U R D A T A 4 | 16
Mei 2, 2021
printf("M ");
}
printf("\n");
}
printf("\nMatriks Jalur (P)\n");
for(i = 0; i < jml_ordo; i++){
for(j = 0; j < jml_ordo; j++){
if(g->P[i][j] != M)
printf("%d ", g->P[i][j]);
else
printf("M ");
}
printf("\n");
}
printf("\nMatriks Rute (R)\n");
for(i = 0; i < jml_ordo; i++){
for(j = 0; j < jml_ordo; j++){
if(g->R[i][j] != M)
printf("%d ", g->R[i][j]);
else
printf("M ");
}
printf("\n");
}
printf("\n");
}
void inisialisasi(stack *s){
s->count = 0;
}
void push(int x, stack *s){
s->data[s->count] = x;
++(s->count);
}
int pop(stack *s){
int temp;
temp = s->data[--s->count];
return(temp);
}
void warshall(typegraph *g){
int i, j, k;
P R A K T I K U M A L G O R I T M A D A N S T R U K T U R D A T A 5 | 16
Mei 2, 2021
inisialisasi(&tumpukan);
printf("Shortest route = ");
for(i = destination; i >= 0; i--){
if(g->R[origin][i] == 0){
jml_tumpukan = tumpukan.count;
printf("%d -", origin + 1);
for(j = 0; j < jml_tumpukan; j++){
pop_rute[j] = pop(&tumpukan);
printf(" %d -", pop_rute[j]);
}
printf(" %d\n\n", destination + 1);
}
else
push(g->R[origin][i], &tumpukan);
}
for(i = 0; i < jml_tumpukan - 1; i++){
beban = beban + pop_rute[i];
}
return beban;
}
P R A K T I K U M A L G O R I T M A D A N S T R U K T U R D A T A 6 | 16
Mei 2, 2021
B. Output
P R A K T I K U M A L G O R I T M A D A N S T R U K T U R D A T A 7 | 16
Mei 2, 2021
2. Graph Dinamis
A. Listing program
#include <stdio.h>
#include <stdlib.h>
#define N 16
#define M 9999
#define MAX 5
typedef struct{
int data[MAX];
int count;
} stack;
typedef struct{
int Q[N][N], P[N][N], R[N][N];
}typegraph;
typegraph graph;
stack tumpukan;
int jml_ordo;
int main()
{
int asal, tujuan, load;
typegraph *g = &graph;
P R A K T I K U M A L G O R I T M A D A N S T R U K T U R D A T A 8 | 16
Mei 2, 2021
return 0;
}
void input(typegraph *g){
int i, j;
P R A K T I K U M A L G O R I T M A D A N S T R U K T U R D A T A 9 | 16
Mei 2, 2021
fflush(stdin);
}
}
printf("\n");
}
void tampilmatrikslama(typegraph *g){
int i, j;
P R A K T I K U M A L G O R I T M A D A N S T R U K T U R D A T A 10 | 16
Mei 2, 2021
s->data[s->count] = x;
++(s->count);
}
int pop(stack *s){
int temp;
P R A K T I K U M A L G O R I T M A D A N S T R U K T U R D A T A 11 | 16
Mei 2, 2021
temp = s->data[--s->count];
return(temp);
}
void warshall(typegraph *g){
int i, j, k;
inisialisasi(&tumpukan);
printf("Shortest route = ");
for(i = destination; i >= 0; i--){
if(g->R[origin][i] == 0){
jml_tumpukan = tumpukan.count;
printf("%d -", origin + 1);
for(j = 0; j < jml_tumpukan; j++){
pop_rute[j] = pop(&tumpukan);
printf(" %d -", pop_rute[j]);
}
printf(" %d\n\n", destination + 1);
}
else
P R A K T I K U M A L G O R I T M A D A N S T R U K T U R D A T A 12 | 16
Mei 2, 2021
push(g->R[origin][i], &tumpukan);
}
for(i = 0; i < jml_tumpukan - 1; i++){
beban = beban + pop_rute[i];
}
return beban;
}
B. Output
P R A K T I K U M A L G O R I T M A D A N S T R U K T U R D A T A 13 | 16
Mei 2, 2021
P R A K T I K U M A L G O R I T M A D A N S T R U K T U R D A T A 14 | 16
Mei 2, 2021
P R A K T I K U M A L G O R I T M A D A N S T R U K T U R D A T A 15 | 16
Mei 2, 2021
P R A K T I K U M A L G O R I T M A D A N S T R U K T U R D A T A 16 | 16