Teknik Informatika - Universitas Muhammadiyah Malang (UMM) GRAPH Oleh : Nur Hayatin, S.ST Teknik Informatika - Universitas Muhammadiyah Malang (UMM) Tahun Akademik 2010-2011
Sub Topik Overview : Graph Jenis Graph Implementasi Graph Graph Property Representasi Graph
Graph
Graph G = (V,E) Dimana : V adalah Vertex, menyatakan entitas data. E adalah Edge, menyatakan garis penghubung antar node. Vertex (Vertices) disebut juga sebagai node atau point. Edge disebut juga sebagai arcs atau lines. Setiap edge menghubungkan dua vertices.
Contoh Graph Contoh persoalan di dunia nyata yang dapat direpresentasikan dengan graph adalah : Jaringan pertemanan pada Facebook.
Jaringan pertemanan pada Facebook Toni Nina Ale Firda Joko Riza Graph dengan 6 node/vertex dan 7 lines/edge yang merepresentasikan jaringan pertemanan pada Facebook
Penjabaran Node(vertex): para pemakai Facebook Lines(edge) : Garis penghubung antara pemakai satu dengan yang lain. Sehingga dari contoh graph diatas dapat dijabarkan sebagai berikut : V = {Nina, Toni, Ale, Riza, Joko, Firda} E = {{Nina,Toni},{Toni,Riza},{Nina, Riza}, {Toni,Ale}, {Ale,Joko},{Riza,Joko},{Firda,Joko}}
Jenis Graph
Jenis Graph Directed Graph (Digraph) Undirected Graph Weigth Graph Complete Undirected Graph Connected Undirected Graph Weigth Graph
Directed Graph (Digraph) Graph yang memiliki orientasi/arah. Setiap lines/edge Digraph memiliki anak panah yang mengarah ke node tertentu. Secara notasi sisi digraph ditulis sebagai vektor (u, v). u = origin (vertex asal) v = terminus (vertex tujuan) u v
Contoh Digraph (1) 2 3 1 4 5 6 7
Contoh Digraph (2) G = {V, E} V = {A, B, C, D, E, F, G, H, I,J, K, L, M} E = {(A,B),(A,C), (A,D), (A,F), (B,C), (B,H), (C,E), (C,G), (C,H), (C,I), (D,E), (D,F), (D,G), (D,K), (D,L), (E,F), (G,I), (G,K), (H,I), (I,J), (I,M), (J,K), (J,M), (L,K), (L,M)}.
Gambar Digraph (2)
Undirected Graph (Digraph) Graph yang tidak memiliki orientasi/arah. Setiap sisi {u, v} berlaku pada kedua arah. Misalnya : {x,y}. Arah bisa dari x ke y, atau y ke x. Secara grafis sisi pada undigraph tidak memiliki mata panah dan secara notasional menggunakan kurung kurawal. u v
Contoh Undi-Graph (1) 2 3 8 10 1 4 5 9 11 6 7
Contoh Undi-Graph (2) G = {V, E} V = {A, B, C, D, E, F, G, H, I,J, K, L, M} E = { {A,B},{A,C}, {A,D}, {A,F}, {B,C}, {B,H}, {C,E}, {C,G}, {C,H}, {C,I}, {D,E}, {D,F}, {D,G}, {D,K}, {D,L}, {E,F}, {G,I}, {G,K}, {H,I}, {I,J}, {I,M}, {J,K}, {J,M}, {L,K}, {L,M}}.
Gambar Undi-Graph (2)
Complete Undirected Graph Semua node memiliki edge/lines untuk setiap node pada graph. n = 2 n = 4 n = 3 n = 1 n = node
Connected Graph Termasuk Jenis Undirected graph. Setiap pasang vertex memiliki edge.
Contoh Connected Graph 2 3 8 10 1 4 5 9 11 6 7 Determine whether an undirected graph is connected.
Contoh Not Connected Graph 2 3 8 10 1 4 5 9 11 6 7
Connected Components 2 3 8 10 1 4 5 9 11 6 7
Weigth Graph Jika semua lines/edge dalam graph memiliki bobot/nilai (value).
Contoh Weigth Graph B A C E F D 2 4 5
Loop Digraph dapat memiliki edge dari dan menuju ke node itu sendiri (Self-edge). Hal ini dikenal dengan istilah loop. 5 3 7 1 4 2 6 Self-edge/loop LEGAL
Implementasi Graph
Aplikasi Jaringan Komunikasi 2 3 8 10 1 4 5 9 11 6 7 Internet connection. Vertices are computers. Send email from 1 to 7. Node/Vertex = kota Lines/Edge = communication link.
Peta Penelusuran Kota (Berdasarkan Jarak/Waktu) 2 3 8 10 1 4 5 9 11 6 7 Node/Vertex = Kota Lines/edge = jalur Weight = jarak/waktu
Peta kota 2 3 8 10 1 4 5 9 11 6 7 Some streets are one way.
Graph Property
Property Jumlah Edge Vertex Degree Jumlah Vertex Degree In-Degree Out-Degree
Jumlah Lines/Edge—Undirected Graph Setiap lines/edge memiliki bentuk (u,v) dimana u != v. Jumlah pasangan vertex n yang mungkin adalah n(n-1). Jumlah lines/edge undirected graph (lebih kecil sama dengan) <= n(n-1)/2. Selama (u,v) sama dengan (v,u) Jumlah lines/edge pada complete undi-graph adalah n(n-1)/2.
Jumlah Lines/Edges—Directed Graph Setiap lines/edge memiliki bentuk (u,v) dimana u != v. Jumlah pasangan verteks yang mungkin untuk vertex n adalah n(n-1). Selama edge(u,v) tidak sama dengan (u,v) dan (v,u) maka jumlah edge untuk complete directed graph adalah n(n-1). Jumlah edge untuk directed graph (lebih kecil sama dengan) <= n(n-1).
Vertex degree Degree : Jumlah edge/lines yang dimiliki node/vertex.
Contoh Vertex Degree degree(2) = 2, degree(5) = 3, degree(3) = 1 8 10 1 4 5 9 11 6 7 degree(2) = 2, degree(5) = 3, degree(3) = 1 degree(9) = ??? degree (11) = ???
Jumlah Vertex Degrees Jumlah Vertex degrees = 2e (e adalah jumlah edge) Jumlah Vertex Degree = 6 8 10 9 11
In-Degree Jumlah edge yang mengarah ke Node/Vertex. indegree(2) = 1, indegree(8) = 0, indegree(7)=??? 2 3 8 10 1 4 5 9 11 6 7
Out-Degree Jumlah edge yang keluar dari Node/Vertex. outdegree(2) = 1, outdegree(8) = 2, outdegree(7) = ??? 2 3 8 10 1 4 5 9 11 6 7
Representasi Graph
Representasi Graph Adjacency Matrix Adjacency Lists
Adjacency Matrix Direpresentasikan dengan array 2D.
Adjacency Matrix Matriks digunakan untuk himpunan adjacency setiap verteks. Baris berisi vertex asal adjacency, sedangkan kolom berisi vertex tujuan adjacency. Bila sisi graph tidak mempunyai bobot, maka [x, y] adjacency disimbolkan dengan 1 dan 0. Bila sisi graph mempunyai bobot, maka [x, y] adjacency disimbolkan dengan bobot sisi tersebut.
Representasi Graph
Penerapan Adjacency Matriks
Adjacency Matrix Representasi Matrik ordo nxn , dimana n = vertex/node A(i,j) = 1, jika antara node i dan node j terdapat edge/terhubung. 1 2 3 4 5 2 3 1 4 5 1 1 1 1 1 1 1 1 1 1
Adjacency Matrix (Undi-graph) 2 3 1 4 5 Diagonal entries are zero. Adjacency matrix of an undirected graph is symmetric. A(i,j) = A(j,i) for all i and j.
Adjacency Matrix (Digraph) 2 3 1 4 5 Adjacency matrix of a digraph need not be symmetric.
Adjacency List Direpresentasikan dengan linked list atau array. Linked Adjacency Lists Array Adjacency Lists
Array Adjacency Lists Adjacency list for vertex i is a linear list of vertices adjacent from vertex i. An array of n adjacency lists. aList[1] = (2,4) aList[2] = (1,5) aList[3] = (5) aList[4] = (5,1) aList[5] = (2,4,3) 2 3 1 4 5
Adjacency Lists (Linked List) Each adjacency list is a chain. 2 3 1 4 5 aList[1] aList[5] [2] [3] [4] 2 4 1 5 5 5 1 2 4 3 Array length n simply means we need an array with n spots. A direct implementation using a Java array would need n+1 spots, because spot 0 would not be utilized. However, by using spot 0 for vertex 1, spot 1 for vertex 2, and so on, we could get by with a Java array whose length is actually n.
Adjacency Lists (Array) Each adjacency list is an array list. 2 3 1 4 5 aList[1] aList[5] [2] [3] [4]
1. Gambarkan undirected graph dari representasi graph berikut: Latihan 1. Gambarkan undirected graph dari representasi graph berikut:
2. Representasikan dengan adjacency list & adjacency matrix B A C E F D 2 4 5
Spanning Tree Subgraph that includes all vertices of the original graph. Subgraph is a tree. If original graph has n vertices, the spanning tree has n vertices and n-1 edges.
Minimum Cost Spanning Tree 2 3 8 10 1 4 5 9 11 6 7 Tree cost is sum of edge weights/costs.
A Spanning Tree Spanning tree cost = 51. 2 4 3 8 8 1 6 10 2 4 5 4 4 3 9 8 11 5 6 2 7 6 7 Spanning tree cost = 51.
Minimum Cost Spanning Tree 2 4 3 8 8 1 6 10 2 4 5 4 4 3 5 9 8 11 5 6 2 In the communication networks area, we are interested in finding minimum cost spanning trees. 7 6 7 Spanning tree cost = 41.
A Wireless Broadcast Tree 2 4 3 8 8 1 6 10 2 4 5 4 4 3 5 9 8 11 5 6 2 Edge cost is power needed to reach a node. If vertex 1 broadcasts with power 2, only vertex 4 is reached. If it broadcasts with power 4, both 2 and 4 are reached. Min-broadcast rooted spanning tree is NP-hard. Cost of tree of previous slide becomes 26. 7 6 7 Source = 1, weights = needed power. Cost = 4 + 8 + 5 + 6 + 7 + 8 + 3 = 41.
Pustaka Sartaj Sahni , “Data Structures & Algorithms”, Presentation L20-24. Mitchell Waite, “Data Structures & Algorithms in Java”, SAMS, 2001