5. rekjar graph-1.pdf
TRANSCRIPT
-
8/10/2019 5. rekjar GRAPH-1.pdf
1/45
1
GRAPH
-
8/10/2019 5. rekjar GRAPH-1.pdf
2/45
2
Informally, a graph is a set of objects called vertices(or nodes)
connected by links called edges(or arcs). Typically, a graph isdepicted 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 theoryis the branch of mathematicsthatexamines the properties of graphs.
http://en.wikipedia.org/wiki/Vertexhttp://en.wikipedia.org/wiki/Edgehttp://en.wikipedia.org/wiki/Mathematicshttp://en.wikipedia.org/wiki/Graph_(mathematics)http://en.wikipedia.org/wiki/Graph_(mathematics)http://en.wikipedia.org/wiki/Mathematicshttp://en.wikipedia.org/wiki/Image:6n-graf.pnghttp://en.wikipedia.org/wiki/Edgehttp://en.wikipedia.org/wiki/Vertex -
8/10/2019 5. rekjar GRAPH-1.pdf
3/45
3
Graphs and Networks
Many problems can be representedby a graphical networkrepresentation.
Examples:
Distribution problems
Routing problems Maximum flow problems
Designing computer / phone / road networks
Equipment replacement
And of course the Internet
Aside: finding the right
problem representationis one of the key issues.
-
8/10/2019 5. rekjar GRAPH-1.pdf
4/45
4
Sub-Category Graph
No Threshold
New Science of Networks
NYS Electric
Power Grid
(Thorp,Strogatz,Watts) Cybercommunities(Automatically discovered)
Kleinberg et al
Network of computer scientists
ReferralWeb System(Kautz and Selman)
Neural network of the
nematode worm C- elegans(Strogatz, Watts)
Networks are
pervasive
Utility Patent network1972-1999
(3 Million patents)Gomes,Hopcroft,Lesser,
Selman
-
8/10/2019 5. rekjar GRAPH-1.pdf
5/45
6
Example: Coloring a Map
How to color this map so that no two
adjacent regions have the same color?
-
8/10/2019 5. rekjar GRAPH-1.pdf
6/45
-
8/10/2019 5. rekjar GRAPH-1.pdf
7/45
8
Example: Coloring a Map
How to color this map so that no two
adjacent regions have the same color?
-
8/10/2019 5. rekjar GRAPH-1.pdf
8/45
9
Example: Graph
-
8/10/2019 5. rekjar GRAPH-1.pdf
9/45
10
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 communicationlinks are always symmetric (same: capacity,propagation delay, traffic volume etc.)
for most communication networks the communication
links are asymmetric, and hence every link isrepresented by two directed edges in oppositedirections
-
8/10/2019 5. rekjar GRAPH-1.pdf
10/45
11
Definitions in graph theory vary in the literature. Here are theconventions used in this encyclopedia.(wikipedia)A directed graph(also called digraphor quiver) consists of
a setVof vertices, and
a set Eof edges, andmapss, t: EV, where s(e) is the sourceand t(e) is the
targetof the directed edge e.
An undirected graph(or graphfor short) is given by
a set Vof vertices,a set Eof edges,
a function w: EP(V)which associates to each edge atwo- or one-element subset of V, interpreted as theendpoints of the edge.
Basic formal definitions
http://en.wikipedia.org/wiki/Sethttp://en.wikipedia.org/wiki/Map_(mathematics)http://en.wikipedia.org/wiki/Power_sethttp://en.wikipedia.org/wiki/Power_sethttp://en.wikipedia.org/wiki/Power_sethttp://en.wikipedia.org/wiki/Power_sethttp://en.wikipedia.org/wiki/Power_sethttp://en.wikipedia.org/wiki/Power_sethttp://en.wikipedia.org/wiki/Map_(mathematics)http://en.wikipedia.org/wiki/Set -
8/10/2019 5. rekjar GRAPH-1.pdf
11/45
12
If the edges have a direction associated with them (indicatedby an arrow in the graphical representation) we have adirected graph (DIGRAPH). This means it is possible tofollow a path from one vertex to another, but not in theopposite direction.
If there are no directed edges, the graph is an undirectedgraph
Directed graph&Undirected graph
An undirected graphwith 6 vertices and 7 edges.
An directed graphwith 3 vertices and 4 edges
http://en.wikipedia.org/wiki/Image:6n-graf.png -
8/10/2019 5. rekjar GRAPH-1.pdf
12/45
13
Directed Graphs
A digraph with 4 vertices and 5 arcsV = {1, 2, 3, 4}
E = {(1,2), (1,3), (2,4), (3,2), (4,3)}
A directed graph or digraph Gcomprises1. a set of vertices V2. 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 wis adjacentto v.
A pathis a sequence of vertices v1, v2,...vn suchthat the vertex pairs (v1, v2),(v2, v3), ..., (vn - 1,vn) are arcs in the graph. The lengthof a path is
the number of arcs on the path. A single vertex vbyitself denotes a path of length zero.
A path v1, v2,..., vn is said to be simpleif thevertices are distinct, except possibly v1 and vn. Ifv1 = vn and n> 1, we call such a path a simplecycle.
-
8/10/2019 5. rekjar GRAPH-1.pdf
13/45
14
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}.
http://en.wikipedia.org/wiki/Image:6n-graf.png -
8/10/2019 5. rekjar GRAPH-1.pdf
14/45
15
trivial graph
The graph with only one vertex and no edgesis the trivial graphor "the dot". A graphwith only vertices and no edges is known asan empty graph; the graph with no verticesand no edges is the Null graph.
Depending on the applications, edges may or may nothave a direction, If the edges have a direction associatedwith them (indicated by an arrow in the graphicalrepresentation) then it is a directed graph, or digraph.
Trivial graph & Digraph
-
8/10/2019 5. rekjar GRAPH-1.pdf
15/45
16
A complete graphis a simple graphwhere anedgeconnects every pair of vertices. Acomplete graph on nverticeshas nverticesand n(n1)/2 edges, and is indicated by thenotation Kn.
complete graph
Complete graphs on nvertices, for nbetween 1 and 6, are shown below:
http://en.wikipedia.org/wiki/Graph_theoryhttp://en.wikipedia.org/wiki/Edgehttp://en.wikipedia.org/wiki/Vertexhttp://en.wikipedia.org/wiki/Edgehttp://en.wikipedia.org/wiki/Image:Graph_k6.jpghttp://en.wikipedia.org/wiki/Image:Graph_k5.jpghttp://en.wikipedia.org/wiki/Image:Graph_k4.jpghttp://en.wikipedia.org/wiki/Image:Graph_k3.jpghttp://en.wikipedia.org/wiki/Image:Graph_k2.jpghttp://en.wikipedia.org/wiki/Image:Graph_k1.jpghttp://en.wikipedia.org/wiki/Edgehttp://en.wikipedia.org/wiki/Vertexhttp://en.wikipedia.org/wiki/Edgehttp://en.wikipedia.org/wiki/Graph_theory -
8/10/2019 5. rekjar GRAPH-1.pdf
16/45
17
A graph is bipartiteif 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 bipartitegraph if every vertex in U isconnected to every vertex in V. If U has nelements and V has m, then
we denote the resulting complete bipartite graph by Kn,m..
Bipartite
The illustration showscomplete bipartite
graph K3,3
The illustration showscomplete bipartite
graph K2,3
-
8/10/2019 5. rekjar GRAPH-1.pdf
17/45
18
Stars and Tripartite Graphs
Stars Tripartite Graphs
-
8/10/2019 5. rekjar GRAPH-1.pdf
18/45
19
In general, there are four ways to represent a graphin a computer system: The incidence list
representation, the incidence matrixrepresentation,
adjacency listrepresentation, and the adjacencymatrixrepresentation.
Graph representations
http://en.wikipedia.org/w/wiki.phtml?title=Incidence_list&action=edithttp://en.wikipedia.org/wiki/Incidence_matrixhttp://en.wikipedia.org/wiki/Adjacency_listhttp://en.wikipedia.org/wiki/Adjacency_matrixhttp://en.wikipedia.org/wiki/Adjacency_matrixhttp://en.wikipedia.org/wiki/Adjacency_matrixhttp://en.wikipedia.org/wiki/Adjacency_matrixhttp://en.wikipedia.org/wiki/Adjacency_listhttp://en.wikipedia.org/wiki/Incidence_matrixhttp://en.wikipedia.org/w/wiki.phtml?title=Incidence_list&action=edit -
8/10/2019 5. rekjar GRAPH-1.pdf
19/45
20
Incidence ListThis representation uses an array. Each element in the arraycorresponds with a single edge. Each edge contains a list ofsize two which corresponds to the endpoints of the edge. This
can also apply to directed graphs by ensuring the first vertexbe defined as the source or destination while the second shouldbe defined as the opposite.
Incidence matrix
This representation uses a matrixof M edges by N vertices. Ifthe vertex is an endpoint to the edge, a value of 1 is assignedto their crossing, otherwise, a value of 0 is assigned. This is aterrible waste of space as every column or row represented bythe edge can only have two values of 1 while the rest are
labelled 0.
Incidence List&Incidence matrix
http://en.wikipedia.org/wiki/Arrayhttp://en.wikipedia.org/wiki/Matrix_(mathematics)http://en.wikipedia.org/wiki/Matrix_(mathematics)http://en.wikipedia.org/wiki/Array -
8/10/2019 5. rekjar GRAPH-1.pdf
20/45
21
Adjacency list- Much like the incidence list, each node has a list ofwhich nodes it is adjacent to. This can sometimes result in "overkill" inan 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 maychoose to use the unneeded space anyway, or he/she may choose tolist the adjacency once. This representation is easier to find all thenodes which are connected to a single node, since these are explicitlylisted.
Adjacency matrix- there is an N by N matrix, where N is the totalnumber of vertices in the graph. If there is an edge from some vertexx to some vertex y, then the element Mx,ywould be 1, otherwise itwould be 0. This makes it easier to find subgraphs, and to reversegraphs if needed.
Adjacency list & Adjacency matrixThese two representations are most useful when information aboutedges is more desirable than information about vertices.
-
8/10/2019 5. rekjar GRAPH-1.pdf
21/45
22
Incidence matrix for undirected graphs
1
4
3
2
c
e
d
gf
h
ba
0 if v is not an endpoint of eIG [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
-
8/10/2019 5. rekjar GRAPH-1.pdf
22/45
23
Incidence matrix for directed graphs
1
4
3
2
c
d
f
e
h
gb a
0 if v is not an endpoint of eID [v,e ]= 1 if v is the headpoint of e
-1 if v is the tail of e2 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 13 0 0 1 1 0 0 0 0
4 0 0 0 -1 0 -1 1 -1
-
8/10/2019 5. rekjar GRAPH-1.pdf
23/45
24
1
4
3
2
c
e
d
gf
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 uvAG [v,e ]=
the number of self loops at v if u = v
-
8/10/2019 5. rekjar GRAPH-1.pdf
24/45
25
1
4
3
2
c
d
f
e
hgb a
Adjacency matrix for directed graphs
1 2 3 4
1 2 0 1 0AG= 2 1 0 0 1
3 0 0 0 0
4 0 2 1 0
the number of arc from u to v if uvAG [v,e ]=
the number of self loops at v if u = v
-
8/10/2019 5. rekjar GRAPH-1.pdf
25/45
26
example
-
8/10/2019 5. rekjar GRAPH-1.pdf
26/45
27
The adjacency matrixof a directed or undirected graph Gwith nvertices is the
n-by-nmatrixMwith
with E(G) the edge set of G. In other words the entry in row iand columnjis one
if there is an edge in the graph with the i-th vertex as source and thej-th vertex
as target.
ExamplesThe adjacency matrix for the example graph
is
Definition
http://en.wikipedia.org/wiki/Matrix_(mathematics)http://en.wikipedia.org/wiki/Image:6n-graf.pnghttp://en.wikipedia.org/wiki/Matrix_(mathematics) -
8/10/2019 5. rekjar GRAPH-1.pdf
27/45
28
a pathin a graphis a sequence of verticessuch thatfrom each of its vertices there is an edgeto thesuccessor vertex.A path is called simple pathif none of the vertices in
the path are repeated.
Two paths are independentif they do not have anyvertex in common, except the first and last one.
The length of a pathis the number of edges that thepath uses, counting multiple edges multiple times. Inthe example graph, (1, 2, 5, 1, 2, 3) is a path withlength 5, and (5, 2, 1) is a simple path of length 2.
Path
http://en.wikipedia.org/wiki/Graph_(mathematics)http://en.wikipedia.org/wiki/Vertex_(graph_theory)http://en.wikipedia.org/wiki/Edge_(graph_theory)http://en.wikipedia.org/wiki/Edge_(graph_theory)http://en.wikipedia.org/wiki/Vertex_(graph_theory)http://en.wikipedia.org/wiki/Graph_(mathematics) -
8/10/2019 5. rekjar GRAPH-1.pdf
28/45
29
elementary path
Repeated edges or vertices within the path arepermissible. When there are no repeated edges in the pathof a directed graph, then the path is called a simplepath. On the other hand, an elementary pathin adirected graph is one in which there are no repeatedvertices within the path. The path in our road mapexample is considered a simple path and also anelementary path.
http://www.cs.usask.ca/resources/tutorials/csconcepts/1999_8/tutorial/definitions.htmlhttp://www.cs.usask.ca/resources/tutorials/csconcepts/1999_8/tutorial/definitions.html -
8/10/2019 5. rekjar GRAPH-1.pdf
29/45
30
cycle
A cycleis a path in which the initial vertex of the path is alsothe 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 repeatedvertices. This means that if a path exists between any twovertices, an elementary path must also exist between thesetwo vertices. When a simple directed graph does not containany cycles is termed acyclic.
-
8/10/2019 5. rekjar GRAPH-1.pdf
30/45
31
Reachability
If a vertex is reachable from another vertex then a pathexists from the one vertex to the other vertex. It isassumed that every vertex is reachable from itself. Also, ifvertex b is reachable from vertex a and vertex c is reachable
from vertex b, then it follows that vertex c is reachable fromvertex a. The definition of reachability holds true for bothdirected and undirected graphs.
-
8/10/2019 5. rekjar GRAPH-1.pdf
31/45
32
Tree & Forest
In graph theory, a treeis a graph inwhich any two verticesare connectedby exactly onepath.
A forestis a graph in which any twovertices are connected by at most onepath. An equivalent definition is that aforest is a disjoint union of trees
A treeis defined to be an undirected, acyclic and connectedgraph (or more simply, a graph in which there is only one pathconnecting each pair of vertices).
http://en.wikipedia.org/wiki/Graph_theoryhttp://en.wikipedia.org/wiki/Vertexhttp://en.wikipedia.org/wiki/Vertexhttp://en.wikipedia.org/wiki/Graph_theoryhttp://en.wikipedia.org/wiki/Image:Tree_graph.png -
8/10/2019 5. rekjar GRAPH-1.pdf
32/45
33
DefinitionsA treeis an undirected simple graph Gthat satisfies any ofthe following equivalentconditions:
Gis connectedand has no simple cyclesGhas no simple cycles and, if any edgeis added to G,then a simple cycle is formedGis connected and, if any edgeis removed from G, then itis not connected anymore Any two vertices in Gcan beconnected by a unique simple path.
If Ghas finitely many vertices, say nof them, then the abovestatements are also equivalent to:
Gis connected and has n 1 edgesGhas no simple cycles and has n 1 edges
Tree (cont)
http://en.wikipedia.org/wiki/Equivalenthttp://en.wikipedia.org/wiki/Connected_graphhttp://en.wikipedia.org/wiki/Cycle_(graph_theory)http://en.wikipedia.org/wiki/Edgehttp://en.wikipedia.org/wiki/Simple_pathhttp://en.wikipedia.org/wiki/Simple_pathhttp://en.wikipedia.org/wiki/Edgehttp://en.wikipedia.org/wiki/Cycle_(graph_theory)http://en.wikipedia.org/wiki/Connected_graphhttp://en.wikipedia.org/wiki/Equivalent -
8/10/2019 5. rekjar GRAPH-1.pdf
33/45
34
Tree
A full binary tree with 3 levels Afull ternary tree with 3levels.
-
8/10/2019 5. rekjar GRAPH-1.pdf
34/45
35
Spanning tree
A spanning treeof a graph is just a subgraph that contains allthe vertices and is a tree. A graph may have many spanning
trees;
1
43
2
Spanning tree
for instance the complete graph on four vertices
has sixteen spanning trees:
-
8/10/2019 5. rekjar GRAPH-1.pdf
35/45
-
8/10/2019 5. rekjar GRAPH-1.pdf
36/45
37
Spanning tree
Given ndifferent vertices, there are
n (n2)
different ways to connect them to make a tree.
If G is bipartit with n vertices of G1 and m vertice ofG2, there are
m(n-1)
.n(m-1)
Example :G1 have 3 vertice, G2 have 2 verticeSpanning tree : 3. 22=12
-
8/10/2019 5. rekjar GRAPH-1.pdf
37/45
38
Jumlah Spanning Tree
Bila G merupakan jaringan terhubung (connected graph)dengan n vertice, maka jumlah spanning tree adalah cofactordari matrix M (yang dibuat dari Adjacency matrixdenganmengganti elemen aii dengan valensi vertice i dan mengganti
elemen aij (ij) dengan -aij
Contoh:
1
4
32
1 2 3 4
1 0 1 0 0
AG= 2 1 0 1 13 0 1 0 1
4 0 1 1 0
1 2 3 4
1 1 -1 0 0
M = 2 -1 3 -1 -13 0 -1 2 -1
4 0 -1 -1 2
-
8/10/2019 5. rekjar GRAPH-1.pdf
38/45
39
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
32 1
4
32 1
4
32
1
4
32
Spanning Tree
-
8/10/2019 5. rekjar GRAPH-1.pdf
39/45
40
Directed Acyclic Graphs
-
8/10/2019 5. rekjar GRAPH-1.pdf
40/45
41
minimum spanning tree
-
8/10/2019 5. rekjar GRAPH-1.pdf
41/45
42
A minimum spanning tree is a spanning tree, but has weightsor lengths associated with the edges, and the total weight of thetree (the sum of the weights of its edges) is at a minimum.
minimum spanning tree
A weighted graph G: The minimum spanning tree fromweighted graph G:
-
8/10/2019 5. rekjar GRAPH-1.pdf
42/45
43
Minimum Spanning Trees application
The standard application is to a problem like phone networkdesign. You have a business with several offices; you want tolease phone lines to connect them up with each other; and thephone company charges different amounts of money to
connect different pairs of cities. You want a set of lines thatconnects all your offices with a minimum total cost. It shouldbe a spanning tree, since if a network isn't a tree you canalways remove some edges and save money.
Telephone companies are particularly interested in minimumspanning trees, because the minimum spanning tree of a set ofsites defines the wiring scheme that connects the sites using aslittle wire as possible
-
8/10/2019 5. rekjar GRAPH-1.pdf
43/45
44
Minimum Spanning Trees
Spanning trees often come up in computer networking. Forinstance, if you have a large local area network with a lot ofswitches, it might be useful to find a minimum spanning treeso that only the minimum number of packets need to be
relayed across the network and multiple copies of the samepacket don't arrive via different paths (remember, any twonodes are connected via only a single path in a spanningtree).
Other real-world problems include laying out electrical grids,
Minimum Spanning Trees application
To explain how to find a Minimum Spanning Tree, we will lookat two algorithms: the Kruskal algorithm and the Primalgorithm.
-
8/10/2019 5. rekjar GRAPH-1.pdf
44/45
45
Minimum Spanning Trees algorithm
To explain how to find a Minimum Spanning Tree, we will lookat two algorithms: the Kruskal algorithm and the Primalgorithm. Both algorithms differ in their methodology, butboth eventually end up with the MST. Kruskal's algorithm
uses edges, and Prims algorithm uses vertexconnections 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 avoidingthe creation of cycles, until (n - 1) edges have been added. (NOTE:
Sometimes two or more edges may have the same cost. The order inwhich the edges are chosen, in this case, does not matter. Different
MSTs may result, but they will all have the same total cost, which willalways be the minimum cost)
-
8/10/2019 5. rekjar GRAPH-1.pdf
45/45
46
LATIHAN