You are on page 1of 107

GRAPH

(Struktur Data dan Algoritma)

Fakultas Teknik UPI Y.A.I - 2016


Graph
Definisi
Contoh Aplikasi Graph
Terminologi : Adjacent & Incident
Adjacent
Pada graph tidah berarah, 2 buah simpul
disebut adjacent bila ada busur yang
menghubungkan kedua simpul tersebut.
Simpul v dan w disebut adjacent.

Pada graph berarah, simpul v disebut


adjacent dengan simpul w bila ada busur
dari w ke v.
Incident
Jika e merupakan busur dengan simpul-
simpulnya adalah v dan w yang ditulis
e=(v,w), maka v dan w disebut “terletak”
pada e, dan e disebut incident dengan v dan
w.
Terminologi :
Undirected Graph & Directed Graph
Example : Undirected & Directed Graph
Terminologi : Weighted Graph
Example : Weighted Graph
Terminologi : Degree of a Vertex
The degree of a vertex is the number of
edges incident to that vertex
For directed graph,
the in-degree of a vertex v is the number of edges
that have v as the head
the out-degree of a vertex v is the number of edges
that have v as the tail
Example : Degree of a Vertex
Terminologi : Path
Terminologi : Path (cont)
Terminologi (cont)
Example : Subgraph
Complete Graph
„ A complete
graph is a graph that has the
maximum number of edges
… for undirected graph with n vertices, the
maximum number of edges is n(n-1)/2
… for directed graph with n vertices, the
maximum
number of edges is n(n-1)
… example: G1 is a complete graph
Example : Complete Graph
DAG
Representasi Graph
Adjacency Matrix
Adjacency Lists
Representasi Graph dalam Adj. Matriks

A B C D E
Representasi Graph dalam Adj. Matriks (cont)
Representasi Graph dalam Adj. Matriks (cont)

Graph Berarah dan Berbobot


Representasi Graph dalam Adj. List
Graph Tidak Berarah
Representasi Graph dalam Adj. List (cont)
Graph Berarah
Operasi pada Graph :
Graph Search Methods
Operasi pada Graph :
Graph Search Methods (cont)
Operasi pada Graph :
Graph Search Methods (cont)
Operasi pada Graph :
Graph Search Methods (cont)
Depth-First Search Algorithm

Algorithm DFS(v); Input: A vertex v in a graph


Output: A labeling of the edges as “discovery” edges and “backedges”
for each edge e incident on v do
if edge e is unexplored then let w be the other endpoint of e
if vertex w is unexplored then label e as a discovery edge
recursively call DFS(w)
else label e as a backedge
Depth-First Search Example
2
3
8
1

4
5
9
10

6
7 11

Start search at vertex 1.


Label vertex 1 and do a depth first search
from either 2 or 4.
Suppose that vertex 2 is selected.
Depth-First Search Example
2
3
8
1

4
5
9
10

6
7 11

Label vertex 2 and do a depth first search


from either 3, 5, or 6.
Suppose that vertex 5 is selected.
Depth-First Search Example
2
3
8
1

4
5
9
10

6
7 11

Label vertex 5 and do a depth first search


from either 3, 7, or 9.
Suppose that vertex 9 is selected.
Depth-First Search Example
2
3
8
1

4
5
9
10

6
7 11

Label vertex 9 and do a depth first search


from either 6 or 8.
Suppose that vertex 8 is selected.
Depth-First Search Example
2
3
8
1

4
5
9
10

6
7 11

Label vertex 8 and return to vertex 9.


From vertex 9 do a DFS(6).
Depth-First Search Example
2
3
8
1

4
5
9
10

6
7 11

Label vertex 6 and do a depth first search from either


4 or 7.
Suppose that vertex 4 is selected.
Depth-First Search Example
2
3
8
1

4
5
9
10

6
7 11

Label vertex 4 and return to 6.


From vertex 6 do a dfs(7).
Depth-First Search Example
2
3
8
1

4
5
9
10

6
7 11

Label vertex 7 and return to 6.


Return to 9.
Depth-First Search Example
2
3
8
1

4
5
9
10

6
7 11

Return to 5.
Depth-First Search Example
2
3
8
1

4
5
9
10

6
7 11

Do a dfs(3).
Depth-First Search Example
2
3
8
1

4
5
9
10

6
7 11

Label 3 and return to 5.


Return to 2.
Depth-First Search Example
2
3
8
1

4
5
9
10

6
7 11

Return to 1.
Depth-First Search Example
2
3
8
1

4
5
9
10

6
7 11

Return to invoking method.


Breadth-First Search

• Visit start vertex and put into a FIFO queue.


• Repeatedly remove a vertex from the queue, visit
its unvisited adjacent vertices, put newly visited
vertices into the queue.
Breadth-First Search Algorithm
Algorithm BFS(s): Input: A vertex s in a graph
Output: A labeling of the edges as “discovery” edges and “cross edges”
initialize container L0 to contain vertex s
i0
while Li is not empty do
create container Li+1 to initially be empty
for each vertex v in Li do
if edge e incident on v do
let w be the other endpoint of e
if vertex w is unexplored then
label e as a discovery edge
insert w into Li+1
else label e as a cross edge
ii+1
Breadth-First Search Example

2
3
8
1

4
5
9
10

6
7 11

Start search at vertex 1.


Breadth-First Search Example

2
3
FIFO Queue
8
1 1
4
5
9
10

6
7 11

Visit/mark/label start vertex and put in a FIFO queue.


Breadth-First Search Example

2
3
FIFO Queue
8
1 1
4
5
9
10

6
7 11

Remove 1 from Q; visit adjacent unvisited vertices;


put in Q.
Breadth-First Search Example

2
3
FIFO Queue
8 2 4
1

4
5
9
10

6
7 11

Remove 1 from Q; visit adjacent unvisited vertices;


put in Q.
Breadth-First Search Example

2
3
FIFO Queue
8 2 4
1

4
5
9
10

6
7 11

Remove 2 from Q; visit adjacent unvisited vertices;


put in Q.
Breadth-First Search Example

2
3
FIFO Queue
8 4 5 3 6
1

4
5
9
10

6
7 11

Remove 2 from Q; visit adjacent unvisited vertices;


put in Q.
Breadth-First Search Example

2
3
FIFO Queue
8 4 5 3 6
1

4
5
9
10

6
7 11

Remove 4 from Q; visit adjacent unvisited vertices;


put in Q.
Breadth-First Search Example

2
3
FIFO Queue
8 5 3 6
1

4
5
9
10

6
7 11

Remove 4 from Q; visit adjacent unvisited vertices;


put in Q.
Breadth-First Search Example

2
3
FIFO Queue
8 5 3 6
1

4
5
9
10

6
7 11

Remove 5 from Q; visit adjacent unvisited vertices;


put in Q.
Breadth-First Search Example

2
3
FIFO Queue
8 3 6 9 7
1

4
5
9
10

6
7 11

Remove 5 from Q; visit adjacent unvisited vertices;


put in Q.
Breadth-First Search Example

2
3
FIFO Queue
8 3 6 9 7
1

4
5
9
10

6
7 11

Remove 3 from Q; visit adjacent unvisited vertices;


put in Q.
Breadth-First Search Example

2
3
FIFO Queue
8 6 9 7
1

4
5
9
10

6
7 11

Remove 3 from Q; visit adjacent unvisited vertices;


put in Q.
Breadth-First Search Example

2
3
FIFO Queue
8 6 9 7
1

4
5
9
10

6
7 11

Remove 6 from Q; visit adjacent unvisited vertices;


put in Q.
Breadth-First Search Example

2
3
FIFO Queue
8 9 7
1

4
5
9
10

6
7 11

Remove 6 from Q; visit adjacent unvisited vertices;


put in Q.
Breadth-First Search Example

2
3
FIFO Queue
8 9 7
1

4
5
9
10

6
7 11

Remove 9 from Q; visit adjacent unvisited vertices;


put in Q.
Breadth-First Search Example

2
3
FIFO Queue
8 7 8
1

4
5
9
10

6
7 11

Remove 9 from Q; visit adjacent unvisited vertices;


put in Q.
Breadth-First Search Example

2
3
FIFO Queue
8 7 8
1

4
5
9
10

6
7 11

Remove 7 from Q; visit adjacent unvisited vertices;


put in Q.
Breadth-First Search Example

2
3
FIFO Queue
8 8
1

4
5
9
10

6
7 11

Remove 7 from Q; visit adjacent unvisited vertices;


put in Q.
Breadth-First Search Example

2
3
FIFO Queue
8 8
1

4
5
9
10

6
7 11

Remove 8 from Q; visit adjacent unvisited vertices;


put in Q.
Breadth-First Search Example

2
3
FIFO Queue
8
1

4
5
9
10

6
7 11

Queue is empty. Search terminates.


BFS vs DFS
BFS vs DFS
BFS vs DFS
Operasi pada Graph :
Spanning Tree

„ A spanning tree is a minimal subgraph G’, such


that V(G’)=V(G) and G’ is connected
„ Weight and MST
„ Either dfs or bfs can be used to create a
spanning tree
… When dfs is used, the resulting spanning tree is
known as a depth first spanning tree
… When bfs is used, the resulting spanning tree is
known as a breadth first spanning tree
Operasi pada Graph :
Minimum Spanning Tree

„ A minimum-cost spanning tree is a spanning


tree of least cost
„ Design strategy – greedy method
… Kruskal’s algorithm
„ Edge by edge
… Prim’s algorithm
„ Span out from one vertex
Operasi pada Graph :
Minimum Spanning Tree (cont)

• weighted connected undirected graph


• spanning tree
• cost of spanning tree is sum of edge costs
• find spanning tree that has minimum cost
Minimum Cost Spanning Tree (cont)
Edge Selection Strategies
• Start with an n-vertex 0-edge forest.
Consider edges in ascending order of cost.
Select edge if it does not form a cycle
together with already selected edges.
 Kruskal’s method.
• Start with a 1-vertex tree and grow it into an
n-vertex tree by repeatedly adding a vertex
and an edge. When there is a choice, add a
least cost edge.
 Prim’s method.
Edge Selection Strategies (cont)
• Start with an n-vertex forest. Each
component/tree selects a least cost edge to
connect to another component/tree.
Eliminate duplicate selections and possible
cycles. Repeat until only 1 component/tree
is left.
 Sollin’s method.
Edge Rejection Strategies
• Start with the connected graph. Repeatedly
find a cycle and eliminate the highest cost
edge on this cycle. Stop when no cycles
remain.
• Consider edges in descending order of cost.
Eliminate an edge provided this leaves
behind a connected graph.
Kruskal’s Method
Kruskal’s Method (cont)
Kruskal’s Method (cont)
Kruskal’s Method (cont)
Kruskal’s Algorithm
Kruskal Algorith
’s m

V1 2 V2
Edge Weight Action
(V1, V4) 4 10
1 - 1 3
(V6, V7) 1 - V3 V4 V5
2 - 2 7
(V1, V2) 4
3 4 2 - 5 8 6
(V
(V 2,, V
V4 )) V6 V7
3 -
1
(V1, V3) 4 -
(V4, V7) 4 -
5 -
(V3, V6)
6 -
(V5, V7)

Ruli Manurung & Ade Fasilkom UI - 2007/2008 – Ganjil – 5


Kruskal Algorith
’s m

V1 2 V2
Edge Weight Action
(V1, V4) 4 10
1 A 1 3
(V6, V7) 1 - V3 V4 V5
2 - 2 7
(V1, V2) 4
3 4 2 - 5 8 6
(V
(V 2,, V
V4 )) V6 V7
3 -
1
(V1, V3) 4 -
(V4, V7) 4 -
5 -
(V3, V6)
6 -
(V5, V7)

Ruli Manurung & Ade Fasilkom UI - 2007/2008 – Ganjil – 5


Kruskal Algorith
’s m

V1 2 V2
Edge Weight Action
(V1, V4) 4 10
1 A 1 3
(V6, V7) 1 A V3 V4 V5
2 -
2 7
(V1, V2) 4
3 4 2 - 5 8 6
(V
(V 2,, V
V4 )) V6 V7
3 - 1
(V1, V3) 4 -
(V4, V7) 4 -
5 -
(V3, V6)
6 -
(V5, V7)

Ruli Manurung & Ade Fasilkom UI - 2007/2008 – Ganjil – 5


Kruskal Algorith
’s m

V1 2 V2
Edge Weight Action
(V1, V4) 4 10
1 A 1 3
(V6, V7) 1 A V3 V4 V5
2 A
2 7
(V1, V2) 4
3 4 2 - 5 8 6
(V
(V 2,, V
V4 )) V6 V7
3 - 1
(V1, V3) 4 -
(V4, V7) 4 -
5 -
(V3, V6)
6 -
(V5, V7)

Ruli Manurung & Ade Fasilkom UI - 2007/2008 – Ganjil – 6


Kruskal Algorith
’s m

V1 2 V2
Edge Weight Action
(V1, V4) 4 10
1 A 1 3
(V6, V7) 1 A V3 V4 V5
2 A
2 7
(V1, V2) 4
2 5 8 6
3 4 A
(V
(V 2,, V
V4 )) V6 V7
3 - 1
(V1, V3) 4 -
(V4, V7) 4 -
5 -
(V3, V6)
6 -
(V5, V7)

Ruli Manurung & Ade Fasilkom UI - 2007/2008 – Ganjil – 6


Kruskal Algorith
’s m

V1 2 V2
Edge Weight Action
(V1, V4) 4 10
1 A 1 3
(V6, V7) 1 A V3 V4 V5
2 A
2 7
(V1, V2) 4
3 4 2 A 5 8 6
(V
(V 2,, V
V4 )) V6 V7
3 R 1
(V1, V3) 4 R
(V4, V7) 4 A
5 -
(V3, V6)
6 -
(V5, V7)

Ruli Manurung & Ade Fasilkom UI - 2007/2008 – Ganjil – 6


Kruskal Algorith
Edge Weight Action
’s (V1, m1 A
V4) 1 A
(V6, 2 A
V7) 2 A
(V1, 3 R V1 2 V2
V2) 4 R 4 10
1 3
(V3, 4 A V5
V3 V4
V4) 5 R 2 7
(V2, 6 A 5 8 4 6
V4) V6 V7
(V1, 1
V3)
(V4,
V7)
(V3,
V6)
(V5,
V7)
Ruli Manurung & Ade Fasilkom UI - 2007/2008 – Ganjil – 6
Kruskal Algorith
Edge Weight Action
’s (V1, m1 A
V4) 1 A
(V6, 2 A
V7) 2 A
(V1, 3 R V1 2 V2
V2) 4 R 1
(V3, 4 A
V3 V4 V5
V4) 5 R 2
(V2, 6 A 4 6
V4) V6 V7
1
(V1,
V3)
(V4,
V7)
(V3,
V6)
(V5,
V7)
Ruli Manurung & Ade Fasilkom UI - 2007/2008 – Ganjil – 6
Prim’s Method
Prim’s Algorithm
Prim’ Algorith
s m
V known dV pV
V1 0 0 0
V1 2 V2
V2 0  0
4 10
V3 0  0 1 3
V4 0  0 V3 V4 V5
V5 0  0
2 7
5 8 4 6
V6 0  0
V6 V7
V7 0  0 1

Ruli Manurung & Ade Fasilkom UI - 2007/2008 – Ganjil – 4


Prim’s
Algorithm

V known d p
V1 1 V V
V1 2 V2
V2 0 0 0 4 10
V3 0  V1 1 3
V3 V4 V5
V4 0  V1
2 7
V5 0  V1
4
V6 5 8 6
0  0
V6 V7
V 0  0 1
7  0

Ruli Manurung & Ade Fasilkom UI - 2007/2008 – Ganjil – 4


Prim’s
Algorithm
V known dV pV
V1 1 0 0 V1 2 V2
V2 0  V1 4 10
V3 0  V4 1 3
1 V3 V4 V5
V4
 V1 2 7
0 
V5 V4 5 8 4 6
0 
V4
V6 V7
V6 0
V7

V4
1

Ruli Manurung & Ade Fasilkom UI - 2007/2008 – Ganjil – 4


Prim’s
Algorithm
V known dV pV
V1 1 0 0 V1 2 V2
V2 1  V1 4 10
V3 0  V4 1 3
1 V3 V4 V5
V4
 V1 2 7
0 
V5 V4 5 8 4 6
0 
V4
V6 V7
V6 0
V7

V4
1

Ruli Manurung & Ade Fasilkom UI - 2007/2008 – Ganjil – 5


Prim’s
Algorithm
V known dV pV
V1 1 0 0 V1 2 V2
V2 1  V1 4 10
V3 1  V4 1 3
1 V3 V4 V5
V4
 V1 2 7
0 
V5 V4 5 8 4 6
0 
V3
V6 V7
V6 0
V7

V4
1

Ruli Manurung & Ade Fasilkom UI - 2007/2008 – Ganjil – 5


Prim’s
Algorithm
V known dV pV
V1 1 0 0 V1 2 V2
V2 1  V1 4 10
V3 1  V4 1 3
1 V3 V4 V5
V4
 V1 2 7
0 
V5 V7 5 8 4 6
0 
V7
V6 V7
V6 1
V7

V4
1

Ruli Manurung & Ade Fasilkom UI - 2007/2008 – Ganjil – 5


Prim’s
Algorithm
V known dV pV
V1 1 0 0 V1 2 V2
V2 1  V1 4 10
V3 1  V4 1 3
1 V3 V4 V5
V4
 V1 2 7
0 
V5 V7 5 8 4 6
1 
V7
V6 V7
V6 1
V7

V4
1

Ruli Manurung & Ade Fasilkom UI - 2007/2008 – Ganjil – 5


Prim’s
Algorithm
V known dV pV
V1 1 0 0 V1 2 V2
V2 1  V1 4 10
V3 1  V4 1 3
1 V3 V4 V5
V4
 V1 2 7
1 
V5 V7 5 8 4 6
1 
V7
V6 V7
V6 1
V7

V4
1

Ruli Manurung & Ade Fasilkom UI - 2007/2008 – Ganjil – 5


Prim’s
Algorithm
V known dV pV
V1 1 0 0 V1 2 V2
V2 1  V1
V3 1  V4 1
1  V3 V4 V5
V4 V1 2
1 
V5 V7 4 6
1 
V7
V6 V7
V6 1  1
V7 V4

Ruli Manurung & Ade Fasilkom UI - 2007/2008 – Ganjil – 5


Sollin’s Method
Sollin’s Method (cont)

You might also like