Minggu 14 Graph.pdf
Transcript of Minggu 14 Graph.pdf
![Page 1: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/1.jpg)
Graph
![Page 2: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/2.jpg)
Graph
• Adalah kumpulan obyek atau aktivitas• Direpresentasikan sebagai kumpulan titik(nodes/vertices) dan garis (arcs/edges)
• Contoh :– Travelling salesman problem– Shortest path problem
![Page 3: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/3.jpg)
Notasi graph
• Garis pada graph dinotasikan sebagaie = [u, v]
dimanae : garisu : titik asalv : titik tujuan
![Page 4: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/4.jpg)
Notasi graph
• Jalur pada graph dinotasikan sebagaiP = (v0, v1, …, vn)
dimanaP : jalurvi : titik jalurn : jumlah titik jalur
![Page 5: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/5.jpg)
Bentuk-bentuk graph
• Connected graph• Isolated graph• Complete graph• Directed graph
![Page 6: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/6.jpg)
Connected graph
![Page 7: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/7.jpg)
Isolated graph
![Page 8: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/8.jpg)
Completed graph
![Page 9: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/9.jpg)
Directed graph
![Page 10: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/10.jpg)
Directed graph
indegree
outdegree
![Page 11: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/11.jpg)
Directed graph
One-way traffic(single path)
Two-way traffic(multi path)
![Page 12: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/12.jpg)
Representasi beban
1
2
3 4
511
3
3
5
2
1
----51---4-2-335-1-2--31154321
![Page 13: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/13.jpg)
Matriks beban
----51---4-2-335-1-2--31154321
MMMMM51MMMM4M2MM335M1MM2MM31M154321Q
#define M … //big integer
![Page 14: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/14.jpg)
Representasi Jalur
1
2
3 4
511
3
3
5
2
1
----5√---4-√-√3√-√-2--√√154321
![Page 15: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/15.jpg)
Matriks Jalur
----5√---4-√-√3√-√-2--√√154321
00000510000401001310100200110154321P
![Page 16: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/16.jpg)
Representasi Rute
1
2
3 4
511
3
3
5
2
1
----50---4-0-030-0-2--00154321
![Page 17: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/17.jpg)
Matriks rute
----50---4-0-030-0-2--00154321
MMMMM50MMMM4M0MM030M0MM2MM00M154321R
#define M … //big integer
![Page 18: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/18.jpg)
Shortest path problemMulti path
(Algoritma Warshall)
i
k
j
11
3
3
Melakukan pengecekanapakah beban langsung Q(i, j) memang lebih kecil daripadabeban melalui titik perantaraQ(i,k)+Q(k,j)
if ((Q(i,k)+Q(k,j))< Q(i, j))Q(i, j) � Q(i,k)+Q(k,j)
![Page 19: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/19.jpg)
1
2
3 4
511
3
3
5
2
1
i
j
Q(1,3) = 3
Q(1,1) + Q(1,3) = M+3Q(1,2) + Q(2,3) = 2Q(1,3) + Q(3,3) = 3+MQ(1,4) + Q(4,3) = M+MQ(1,5) + Q(5,3) = M+M
Q(1,3) = 2
Beban langsung
Beban melalui perantara
![Page 20: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/20.jpg)
Algoritma Warshall(untuk beban)
for k=1 to nfor i=1 to n
for j=1 to nif ((Q(i,k)+Q(k,j)) < Q(i,j))
Q(i,j) � (Q(i,k)+Q(k,j)
![Page 21: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/21.jpg)
Algoritma Warshall(untuk jalur)
for k=1 to nfor i=1 to n
for j=1 to nP(i,j) � P(i,j) OR (P(i,k) AND P(k,j))
![Page 22: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/22.jpg)
Pencarian rute
for k=1 to nfor i=1 to n
for j=1 to nif ((Q(i,k) + Q(k,j)) < Q(i,j)) {if (R(i,j) = 0)
R(i,j) �kelse
R(i,j) � R(k,j)}
![Page 23: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/23.jpg)
Cara membaca matriks rute
1
2
3 4
511
3
3
5
2
1
MMMMM50MMMM440210343033243203154321R
Rute 1-5 ?
![Page 24: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/24.jpg)
Rute 1-5?Ambil nilai di baris 1, kolom 5 = 4 � pushAmbil nilai di baris 1, kolom 4 = 3 � pushAmbil nilai di baris 1, kolom 3 = 2 � pushAmbil nilai di baris 1, kolom 2 = 0 (stop) � pop
Rute =1 – 2 – 3 – 4 - 5
4
3
2
![Page 25: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/25.jpg)
Shortest path problemSingle path
(Algoritma Dijkstra)
1
2
3 4
511
3
3
5
2
1
• Tentukan titik asal dantitik tujuan sebelumproses
• Akumulasikan jarakminimal dan simpan ketitik berikutnya.
• Lakukan dari titik asalsampai titik tujuan
![Page 26: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/26.jpg)
1
2
3 4
511
3
3
5
2
1
Titik asal = 1Titik tujuan = 5
x
zy nodesebelumnya
namanodebeban
minimal
![Page 27: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/27.jpg)
2
0M
3
0M
1
00
5
0M
4
0M
1
1
3
3
5
2
1
• Inisialisasi• Baca Node 1• Total beban min = 0• Node sblmnya = 0
![Page 28: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/28.jpg)
2
11
3
13
1
00
5
0M
4
0M
1
1
3
3
5
2
1
3 2
• Node 1 : DONE• Baca node 2 & 3• Total beban min : 2 ����1 & 3 ���� 3
• Node sblmnya : 1
![Page 29: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/29.jpg)
2
11
3
22
1
00
5
26
4
0M
1
1
3
3
5
2
1
5 3
• Node 2 : DONE• Baca node 3 & 5• Total beban min : 3 ���� 2& 5 ���� 6
• Node sblmnya : 2
![Page 30: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/30.jpg)
2
11
3
22
1
00
5
26
4
34
1
1
3
3
5
2
1
4 5
• Node 3 : DONE• Baca node 0 & 4• Beban min node 0 : 5 > 0���� tidak berubah• Total beban min : 4 ���� 4• Node sblmnya : 3
![Page 31: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/31.jpg)
2
11
3
22
1
00
5
45
4
34
1
1
3
3
5
2
1
5
• Node 4 : DONE• Baca node 5• Beban minimal : 5• Node sblmnya : 4
DONE
![Page 32: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/32.jpg)
2
11
3
22
1
00
5
45
4
34
1
1
3
3
5
2
1
Rute : 1 – 2 – 3 – 4 – 5 dengan beban minimal = 5
![Page 33: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/33.jpg)
Representasi Graph dengan Linked List
![Page 34: Minggu 14 Graph.pdf](https://reader033.fdocuments.us/reader033/viewer/2022052515/588757481a28abe31b8c6681/html5/thumbnails/34.jpg)
Representasi Graph dengan Linked List
struct garis{float bobot;struct simpul *ketitik;struct garis *kegaris;
};struct simpul{
char namatitik;struct simpul *titikberikut;struct garis *ptrgaris;
};