graphs representation algorithmscir.dcs.uni-pannon.hu/cikkek/graphs.pdf · a path from v to u is a...

55
Graphs: Representation and Algorithms Terminology : V = Set of vertices (or nodes) |V| = # of vertices or cardinality of V (in usual terminology |V| = n) E = Set of edges, where an edge is defined by two vertices |E| = # of edges or cardinality of E A Graph, G is a pair G = (V, E)

Upload: others

Post on 22-May-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Graphs: Representation and Algorithms

Terminology :

• V = Set of vertices (or nodes) • |V| = # of vertices or cardinality of V (in

usual terminology |V| = n) • E = Set of edges, where an edge is defined

by two vertices • |E| = # of edges or cardinality of E • A Graph, G is a pair G = (V, E)

Page 2: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Labeled Graphs:

We may give edges and vertices labels.

Edges might also be numerically labeled. For instance if the vertices represent cities, the edges might be labeled to represent distances.

Directed Graphs:

• A directed graph is one in which every edge (u, v) has a direction, so that (u, v) is different from (v, u). In an undirected graph, there is no distinction between (u, v) and (v, u).

A Directed Graph

Page 3: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Subgraph:

A subgraph of (V, E) is a pair (V', E'), where V' is a subset of V, and E' consists of all edges (u, v) of E between two nodes of V'.

Connected Component:

A connected component containing the node u is the subgraph consisting of all nodes v that may be reached from u by traversing zero or more edges. A graph may consist of one or more connected components.

Neighbor:

The neighbors of v are all u such that u v, and (u, v) E. In this case we can also say that u and v are adjacent.

Degree:

The degree of v V is equal to its number of neighbors.

Page 4: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Path :

A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path in which each ui is distinct.

Cycle:

A cycle is a path that begins and ends at the same place. In other words its a path from u to u. The Subpath not including the terminal node is simple.

Free Tree:

A free tree is a connected graph without a cycle.

A Free Tree

Page 5: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Q: How many edges are there in a free tree? Ans: |E| = |V| -1

Properties of Graphs

2) # of graphs with |V| vertices : 2 [|V| ( |V|-1 )]/2

3) degrees : d1, d2, . . . , d|V|

Note: This shows that the number of nodes with an odd degree must be even.

Page 6: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Adjacency Matrix Implementation

A |V| × |V| matrix of 0's and 1's.

A 1 represents a connection or an edge. Storage = |V|²

Adjacency Matrix

For a non-directed graph there will always be symmetry along the top left to bottom right diagonal. This diagonal will always be filled with zero's. These facts simplify coding.

Page 7: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Coding a Graph

Coding is concerned with storing the graph in an effecient manner. One might grab all the bits from the adjacency matrix and concatenate them to form a binary string. One could say that the numerical value of the binary string (consider as the positive integer) is the number of the graph. Different graphs have different numbers. Clearly, |V|² bits would suffice.

Graph number= 46.

Coding Graphs

Page 8: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Adjacency List Implementation

The Adjacency list implementation typically uses less space than the adjacency matrix implementation.

This method is very good for problems that involve traversing a graph.

Storage = |V| header cells + 2|E| linked list cells (since each edge in an undirected graph is counted by both vertices that it connects). In a directed graph the 2|E| is replaced by |E|.

Page 9: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Special Kinds of Graphs

Grid:

The neighbours of (i, j) are (i ±1, j) and (i, j ± 1), therefore it suffices to store the pair (i, j). Complete Graph: A complete graph Kn is a graph in which all possible edges are present. The matrix for this graph would contain all 1's therefore there would be no point in storing it.

Page 10: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Complete Bipartite Graph:

A complete bipartite graph Km, n is a graph that has its set of vertices positioned into two subsets, one of size m and the other of size n. All possible edges from one subset to the other are present, but there are no edges between the vertices of the same subset.

Page 11: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Cycle:

The cycle Cn consists of n vertices v1, v2, ..., vn which are connected by the edges (v1, v2), (v2, v3), ....., (vn-1, vn), (vn, v1). Wheel :

The wheel Wn is constructed from the Cycle Cn by adding a new vertex to the graph and connecting it to each of each of the already existing vertices.

Page 12: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Möbius Wheel:

The mobius wheel Mn (where n is even) is constructed from the cycle Cn by adding additional edges between vertices which are directly across from each other.

A sink is a node with |V|-1 ingoing edges and no outgoing edges.

Page 13: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Applications of Graphs

Some of the applications of graphs are :

• Mazes (using stacks) • Networks (computer, cities ....) • Maps (any geographic databases ) • Graphics : Geometrical Objects • Neighborhood graphs • Voronoi diagrams

Graphics :

Many geometrical objects such as cubes, polyhedra, and wire frame car models, may be thought of as graphs. These graphs are more than just nodes and edges. Their geometrical structure must also be taken into account.

Page 14: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

This structure contains three kinds of objects

• vertices • edges • faces

Edges are crucial since, in a three dimensional object, an edge will always belong to only two faces and two nodes. For this reason it makes sense to number the edges. Faces become linked list of edges, and each edge lives in only two faces.

List of Edges Face1 Face2 . . .

Page 15: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

1 2 - 2 3 - 3 4 5 4 1 - 5 - 6 6 - 7 7 . 3 . . . . . . . . . . . .

Faces become linked list of edges, and each edge lives in only two faces

Neighborhood Graphs

Page 16: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

These are graphs for collection of points in d-dimensional space. Such point sets may be visualized by connecting close points. There are many possible definitions of closeness. For example, one might draw a circle with two data points at diametrically opposite sides. If the circle contains no other data points then the two points may be considered "close" and an edge may be added between them.

Page 17: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

MINIMUM SPANNING TREE

We are given as a starting point a weighted connected graph(V,E), in which edges have weights or lengths.

Spanning tree: a free tree on V (thus having |V|-1 edges that are a subset of|E|). Minimum Spanning tree: the spanning tree with minimal total weight, where the weights of the edges picked are summed to obtain a total weight. This is a minimum spanning tree

Page 18: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Applications - Network design: Design networks that connect n sites at a minimal total cost. Clearly, edge weights now represent costs of connections.

- Visualizing multidimensional data: As points that are close to each other are connected, multidimensional data (vectors) may be connected by an MST to see how they relate to each other. Each point on the figure below represents a high-dimensional vector of observations or measurements

Page 19: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

From such a graphs or others like it, one may deduce patterns in evolution. For example, by removing the longest edge in the MST, one obtains two groups, also called clusters. If we remove the k longest edges, we obtain k+1 clusters. This may be useful for clustering objects.

- Clustering (Classification, Taxonomy)

Make the objects into vectors,

Find the EMST (Euclidean Minimal Spanning Tree: nodes V represent a n-dimensional vector, edge weights are Euclidean distances between vectors),

Remove the k longest edges. This yields the grouping as shown in the mammal's milk example.

Page 20: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Mammal's Milk ANIMAL WATER PROTEIN FAT LACTOSE

HORSE 90.1 2.6 1.0 6.9 DONKEY 90.3 1.7 1.4 6.2

MULE 90.0 2.0 1.8 5.5

CAMEL 87.7 3.5 3.4 4.8 LLAMA 86.5 3.9 3.2 5.6 ZEBRA 86.2 3.0 4.8 5.3

SHEEP 82.0 5.6 6.4 4.7

BUFFALO 82.1 5.9 7.9 4.7 FOX 81.6 6.6 5.9 4.9 PIG 82.8 7.1 5.1 3.7

RABBIT 71.3 12.3 13.1 1.9

RAT 72.5 9.2 12.6 3.3

DEER 65.9 10.4 19.7 2.6 REINDEER 64.8 10.7 20.3 2.5

WHALE 64.8 11.1 21.2 1.6

Page 21: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Graph Implementation – Adjacency Matrix

Page 22: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Adjacency Matrix for Weighted Graphs

Page 23: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Graph Implementation – Adjacency List

Page 24: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path
Page 25: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Depth-First Search (DFS)

dfs(in v:Vertex) { mark v as visited for (each unvisited vertex u adjacent to v) dfs(u) }

Page 26: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

DFS Algorithm Application

Page 27: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

BFS Algorithm Application

Page 28: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Minimum Spanning Tree (MST)

Assume the weight of an edge represents a cost value

What is the cheapest solution (tree) that connects all vertices?

Page 29: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Minimum Spanning Tree (MST)

Prim's Algorithm

start at any vertex (= root) mark root as visited and add it to the MST while (there are still unvisited vertices) { find the least-cost edge (v,u) from a visited vertex v

to some unvisited vertex u mark u as visited add vertex u and edge (v,u) to MST }

Page 30: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

MST Example

Page 31: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

MST Example

Page 32: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

MST Example

Page 33: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

MST Example

root

order of inclusion: a, i, f, g, d, h, c, e,

Page 34: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path
Page 35: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path
Page 36: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path
Page 37: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path
Page 38: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path
Page 39: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path
Page 40: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path
Page 41: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path
Page 42: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path
Page 43: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path
Page 44: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path
Page 45: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path
Page 46: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path
Page 47: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path
Page 48: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path
Page 49: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path
Page 50: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path
Page 51: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Kruskal's algorithm. Step 1. In the graph, the Edge(g, h) is shortest. Either vertex g or vertex h could be representative. Lets choose vertex g arbitrarily.

Step 2. The edge (c, i) creates the second tree. Choose vertex c as representative for second tree.

Page 52: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Step 3. Edge (g, f) is the next shortest edge. Add this edge and choose vertex g as representative.

Step 4. Edge (a, b) creates a third tree.

Page 53: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Step 5. Add edge (c, f).

Step 6. Edge (g, i) is the next cheapest, but if we add this edge a cycle would be created. Vertex c is the representative of both.

Step 7. Instead, add edge (c, d).

Page 54: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Step 8. If we add edge (h, i), edge(h, i) would make a cycle.

Step 9. Instead of adding edge (h, i) add edge (a, h).

Page 55: Graphs Representation Algorithmscir.dcs.uni-pannon.hu/cikkek/Graphs.pdf · A path from v to u is a collection u1, u2, . . . uk, v such that all (ui, ui+1) E. A simple path is a path

Step 10. Again, if we add edge (b, c), it would create a cycle. Add edge (d, e) instead to complete the spanning tree. In this spanning tree all trees joined and vertex c is a sole representative.