Top Banner
1 GRAPH
40
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: BAB 2 GRAPH

1

GRAPH

Page 2: BAB 2 GRAPH

2

Informally, a graph is a set of objects called vertices (or nodes)

connected by links called edges (or arcs). Typically, a graph is

depicted as a set of dots (i.e., vertices) connected by lines (i.e.,

edges).

A graph with 6 vertices and 7 edges.

Graph theory

Graph theory is the branch of mathematics that examines the properties of graphs.

Page 3: BAB 2 GRAPH

3

Page 4: BAB 2 GRAPH

4

network model

network model: weighted graph

network can be modelled as a graph (V,E)

• nodes (V) : switches, routers, and hosts

• edges (E) : communication links

• the edges are unidirected only if the communication links are always symmetric (same: capacity, propagation delay, traffic volume etc.)

for most communication networks the communication links are asymmetric, and hence every link is represented by two directed edges in opposite directions

Page 5: BAB 2 GRAPH

5

Definitions in graph theory vary in the literature. Here are the conventions used in this encyclopedia.(wikipedia) A directed graph (also called digraph or quiver) consists of

a set V of vertices, and a set E of edges, and

maps s, t : E → V, where s(e) is the source and t(e) is the

target of the directed edge e.

An undirected graph (or graph for short) is given by a set V of vertices, a set E of edges, a function w : E → P(V) which associates to each edge a

two- or one-element subset of V, interpreted as the endpoints of the edge.

Basic formal definitions

Page 6: BAB 2 GRAPH

6

Formal definition: V={1,2,3,4,5,6}, E={e1,e2,e3,e4,e5,e6,e7}

Basic formal definitions

and the function w(e1)={1,2}, w(e2)={2,3}, w(e3)={1,5}, w(e4)={2,5}, w(e5)={3,4}, w(e6)={4,5}, w(e7)={4,6}.

Page 7: BAB 2 GRAPH

7

•trivial graph

The graph with only one vertex and no edges is the trivial graph or "the dot". A graph with only vertices and no edges is known as an empty graph; the graph with no vertices and no edges is the Null graph.

Depending on the applications, edges may or may not have a direction, If the edges have a direction associated with them (indicated by an arrow in the graphical representation) then it is a directed graph, or digraph.

Trivial graph & Digraph

Page 8: BAB 2 GRAPH

8

If the edges have a direction associated with them (indicated by an arrow in the graphical representation) we have a directed graph (DIGRAPH). This means it is possible to follow a path from one vertex to another, but not in the opposite direction. If there are no directed edges, the graph is an undirected graph

Directed graph & Undirected graph

An undirected graph with 6 vertices and 7 edges.

An directed graph with 3 vertices and 4 edges.

Page 9: BAB 2 GRAPH

9

Directed Graphs

A digraph with 4 vertices and 5 arcs V = {1, 2, 3, 4}

E = {(1,2), (1,3), (2,4), (3,2), (4,3)}

A directed graph or digraph G comprises 1. a set of vertices V 2. a set of directed edges or arcs, E (an arc is an ordered pair of vertices)

If there is an arc (v, w), we say w is adjacent to v. A path is a sequence of vertices v1, v2,...vn such that the vertex pairs (v1, v2),(v2, v3), ..., (vn - 1, vn) are arcs in the graph. The length of a path is the number of arcs on the path. A single vertex v by itself denotes a path of length zero. A path v1, v2,..., vn is said to be simple if the vertices are distinct, except possibly v1 and vn. If v1 = vn and n > 1, we call such a path a simple cycle.

Page 10: BAB 2 GRAPH

10

A complete graph is a simple graph where an edge connects every pair of vertices. A complete graph on n vertices has n vertices and n(n−1)/2 edges, and is indicated by the notation Kn.

complete graph

Complete graphs on n vertices, for n between 1 and 6, are shown below:

Page 11: BAB 2 GRAPH

11

A graph is bipartite if its vertices can be partitioned into two disjoint

subsets U and V such that each edge connects a vertex from U to one

from V.

A bipartite graph is a complete bipartite graph if every vertex in U is

connected to every vertex in V. If U has n elements and V has m, then

we denote the resulting complete bipartite graph by Kn,m..

Bipartite

The illustration shows complete bipartite graph K3,3

The illustration shows complete bipartite graph K2,3

Page 12: BAB 2 GRAPH

12

Stars and Tripartite Graphs

Stars Tripartite Graphs

Page 13: BAB 2 GRAPH

13

In general, there are four ways to represent a graph in a computer system: The incidence list

representation, the incidence matrix representation, adjacency list representation, and the adjacency

matrix representation.

Graph representations

Page 14: BAB 2 GRAPH

14

•Incidence List

This representation uses an array. Each element in the array corresponds with a single edge. Each edge contains a list of size two which corresponds to the endpoints of the edge. This can also apply to directed graphs by ensuring the first vertex be defined as the source or destination while the second should be defined as the opposite.

•Incidence matrix

This representation uses a matrix of M edges by N vertices. If the vertex is an endpoint to the edge, a value of 1 is assigned to their crossing, otherwise, a value of 0 is assigned. This is a terrible waste of space as every column or row represented by the edge can only have two values of 1 while the rest are labelled 0.

Incidence List & Incidence matrix

Page 15: BAB 2 GRAPH

15

Adjacency list - Much like the incidence list, each node has a list of which nodes it is adjacent to. This can sometimes result in "overkill" in an undirected graph as vertex 3 may be in the list for node 2, then node 2 must be in the list for node 3. Either the programmer may choose to use the unneeded space anyway, or he/she may choose to list the adjacency once. This representation is easier to find all the nodes which are connected to a single node, since these are explicitly listed.

Adjacency matrix - there is an N by N matrix, where N is the total number of vertices in the graph. If there is an edge from some vertex x to some vertex y, then the element Mx,y would be 1, otherwise it would be 0. This makes it easier to find subgraphs, and to reverse graphs if needed.

Adjacency list & Adjacency matrix

These two representations are most useful when information about edges is more desirable than information about vertices.

Page 16: BAB 2 GRAPH

16

•Incidence matrix for undirected graphs

1

4

3

2

c

e

d

g f

h

b a

0 if v is not an endpoint of e IG [v,e ]= 1 if v is an endpoint of e 2 if e is s self-loop at v

a b c d e f g h

1

IG= 2

3

4

Page 17: BAB 2 GRAPH

17

•Incidence matrix for directed graphs

1

4

3

2

c

d

f

e

h g

b a

0 if v is not an endpoint of e ID [v,e ]= 1 if v is the headpoint of e -1 if v is the tail of e 2 if e is s self-loop at v

a b c d e f g h

1 2 2 -1 0 1 0 0 0

IG= 2 0 0 0 0 -1 1 -1 1

3 0 0 1 1 0 0 0 0

4 0 0 0 -1 0 -1 1 -1

Page 18: BAB 2 GRAPH

18

1

4

3

2

c

e

d

g f

h

b a

Adjacency matrix for undirected graphs

1 2 3 4

1 2 1 1 0

AG= 2 1 0 0 3

3 1 0 0 1

4 0 3 1 0

the number of edges between u and v if u≠v AG [v,e ]= the number of self loops at v if u = v

Page 19: BAB 2 GRAPH

19

1

4

3

2

c

d

f

e

h g

b a

Adjacency matrix for directed graphs

1 2 3 4

1 2 0 1 0

AG= 2 1 0 0 1

3 0 0 0 0

4 0 2 1 0

the number of arc from u to v if u≠v AG [v,e ]= the number of self loops at v if u = v

Page 20: BAB 2 GRAPH

20

example

Page 21: BAB 2 GRAPH

21

The adjacency matrix of a directed or undirected graph G with n vertices is the

n-by-n matrix M with

with E(G) the edge set of G. In other words the entry in row i and column j is one

if there is an edge in the graph with the i-th vertex as source and the j-th vertex

as target.

Examples The adjacency matrix for the example graph

is

Definition

Page 22: BAB 2 GRAPH

22

a path in a graph is a sequence of vertices such that from each of its vertices there is an edge to the successor vertex. A path is called simple path if none of the vertices in the path are repeated. Two paths are independent if they do not have any vertex in common, except the first and last one. The length of a path is the number of edges that the path uses, counting multiple edges multiple times. In the example graph, (1, 2, 5, 1, 2, 3) is a path with length 5, and (5, 2, 1) is a simple path of length 2.

Path

Page 23: BAB 2 GRAPH

23

elementary path

Repeated edges or vertices within the path are permissible. When there are no repeated edges in the path of a directed graph, then the path is called a simple path. On the other hand, an elementary path in a directed graph is one in which there are no repeated vertices within the path. The path in our road map example is considered a simple path and also an elementary path.

Page 24: BAB 2 GRAPH

24

cycle

A cycle is a path in which the initial vertex of the path is also the terminal vertex of the path. So by removing all the cycles, the path can be considered an elementary path since there will no longer be any repeated vertices. This means that if a path exists between any two vertices, an elementary path must also exist between these two vertices. When a simple directed graph does not contain any cycles is termed acyclic.

Page 25: BAB 2 GRAPH

25

Reachability

If a vertex is reachable from another vertex then a path exists from the one vertex to the other vertex. It is assumed that every vertex is reachable from itself. Also, if vertex b is reachable from vertex a and vertex c is reachable from vertex b, then it follows that vertex c is reachable from vertex a. The definition of reachability holds true for both directed and undirected graphs.

Page 26: BAB 2 GRAPH

26

Tree & Forest

In graph theory, a tree is a graph in which any two vertices are connected by exactly one path. A forest is a graph in which any two vertices are connected by at most one path. An equivalent definition is that a forest is a disjoint union of trees

A tree is defined to be an undirected, acyclic and connected graph (or more simply, a graph in which there is only one path connecting each pair of vertices).

Page 27: BAB 2 GRAPH

27

Definitions A tree is an undirected simple graph G that satisfies any of the following equivalent conditions:

G is connected and has no simple cycles G has no simple cycles and, if any edge is added to G, then a simple cycle is formed G is connected and, if any edge is removed from G, then it is not connected anymore Any two vertices in G can be connected by a unique simple path.

If G has finitely many vertices, say n of them, then the above statements are also equivalent to:

G is connected and has n − 1 edges G has no simple cycles and has n − 1 edges

Tree (cont)

Page 28: BAB 2 GRAPH

28

Tree

A full binary tree with 3 levels A full ternary tree with 3 levels.

Page 29: BAB 2 GRAPH

29

Spanning tree

A spanning tree of a graph is just a subgraph that contains all the vertices and is a tree. A graph may have many spanning

trees;

1

4 3

2

Spanning tree

for instance the complete graph on four vertices

has sixteen spanning trees:

Page 30: BAB 2 GRAPH

30

Spanning tree

1

4 3

2 1

4 3

2 1

4 3

2 1

4 3

2

1

4 3

2 1

4 3

2

1

4 3

2

1

4 3

2 1

4 3

2 1

4 3

2 1

4 3

2

1

4 3

2

1

4 3

2 1

4 3

2 1

4 3

2 1

4 3

2

Page 31: BAB 2 GRAPH

31

Spanning tree

Given n different vertices, there are

n (n−2) different ways to connect them to make a tree. If G is bipartit with n vertices of G1 and m vertice of G2, there are

m(n-1).n(m-1)

Example : G1 have 3 vertice, G2 have 2 vertice Spanning tree : 3. 22=12

Page 32: BAB 2 GRAPH

32

Jumlah Spanning Tree

Bila G merupakan jaringan terhubung (connected graph) dengan n vertice, maka jumlah spanning tree adalah cofactor dari matrix ‘M’ (yang dibuat dari Adjacency matrix dengan mengganti elemen aii dengan valensi vertice i dan mengganti elemen aij (i≠j) dengan -aij

Contoh:

1

4

3 2

1 2 3 4

1 0 1 0 0

AG= 2 1 0 1 1

3 0 1 0 1

4 0 1 1 0

1 2 3 4

1 1 -1 0 0

M = 2 -1 3 -1 -1

3 0 -1 2 -1

4 0 -1 -1 2

Page 33: BAB 2 GRAPH

33

Cofactor:

3 -1 -1 -1 2 -1

-1 -1 2

(-1)1+1 -1 2

-1 -1

-1 -1

-1 2

2 -1

-1 2 = 3 + 1 -1

= 3.3+1.-3-1.3=9-3-3=3

1

4

3 2 1

4

3 2 1

4

3 2

1

4

3 2

Spanning Tree

Page 34: BAB 2 GRAPH

34

Directed Acyclic Graphs

Page 35: BAB 2 GRAPH

35

minimum spanning tree

Page 36: BAB 2 GRAPH

36

A minimum spanning tree is a spanning tree, but has weights or lengths associated with the edges, and the total weight of the tree (the sum of the weights of its edges) is at a minimum.

minimum spanning tree

A weighted graph G: The minimum spanning tree from weighted graph G:

Page 37: BAB 2 GRAPH

37

Minimum Spanning Trees application

The standard application is to a problem like phone network design. You have a business with several offices; you want to lease phone lines to connect them up with each other; and the phone company charges different amounts of money to connect different pairs of cities. You want a set of lines that connects all your offices with a minimum total cost. It should be a spanning tree, since if a network isn't a tree you can always remove some edges and save money.

Telephone companies are particularly interested in minimum spanning trees, because the minimum spanning tree of a set of sites defines the wiring scheme that connects the sites using as little wire as possible

Page 38: BAB 2 GRAPH

38

Minimum Spanning Trees

Spanning trees often come up in computer networking. For instance, if you have a large local area network with a lot of switches, it might be useful to find a minimum spanning tree so that only the minimum number of packets need to be relayed across the network and multiple copies of the same packet don't arrive via different paths (remember, any two nodes are connected via only a single path in a spanning tree). Other real-world problems include laying out electrical grids,

Minimum Spanning Trees application

To explain how to find a Minimum Spanning Tree, we will look at two algorithms: the Kruskal algorithm and the Prim algorithm.

Page 39: BAB 2 GRAPH

39

Minimum Spanning Trees algorithm

To explain how to find a Minimum Spanning Tree, we will look at two algorithms: the Kruskal algorithm and the Prim algorithm. Both algorithms differ in their methodology, but both eventually end up with the MST. Kruskal'’s algorithm uses edges, and Prim’s algorithm uses vertex connections in determining the MST. Kruskal's Algorithm: This is a greedy algorithm. A greedy algorithm chooses some local

optimum (ie. picking an edge with the least weight in a MST). Kruskal'’s algorithm works as follows: Take a graph with 'n'

vertices, keep adding the shortest (least cost) edge, while avoiding the creation of cycles, until (n - 1) edges have been added. (NOTE:

Sometimes two or more edges may have the same cost. The order in which the edges are chosen, in this case, does not matter. Different

MSTs may result, but they will all have the same total cost, which will always be the minimum cost)

Page 40: BAB 2 GRAPH

40

SOAL