graph application of graphs representation of graphs graph traversal directed graphs weighted graph...

35
Graph Application Of Graphs Representation of Graphs Graph Traversal Directed Graphs Weighted Graph Shortest Path Minimum Spanning Tree Data Structure and File Processing

Upload: arthur-roberts

Post on 27-Dec-2015

235 views

Category:

Documents


1 download

TRANSCRIPT

GraphApplication Of Graphs

Representation of GraphsGraph TraversalDirected GraphsWeighted Graph

Shortest PathMinimum Spanning Tree

Data Structure and File Processing

Graphs

Introduction of GraphA graph G consists of two things:1. A set V of elements called nodes(or points or

vertices)2. A set E of edges such that each edge e in E is

identified with a unique (unordered) pair [u, v] of nodes in V, denotes by e = [u, v]

2

Graphs cont..

• If the element are in ordered pairs it is call directed graph.

• If the pairs represent the same edge it is known as undirected graph.

• Subgraph happened when every vertex include in other vertex and every edge is include in other edge.

3

1 2

3 4

5

1 2

3 4

5

4

Directed Graphs

A directed graph.The number of edges with one endpoint on a given vertex is called that vertex's degree. In a directed graph, the number of edges that point to a given vertex is called its in-degree, and the number that point from it is called its out-degree. Often, we may want to be able to distinguish between different nodes and edges. We can associate labels with either. We call such a graph labeled.

5

Directed Graphs cont..Directed Graph Operations• make-graph(): graph• Create a new graph, initially with no nodes or edges.• make-vertex(graph G, element value): vertex• Create a new vertex, with the given value.• make-edge(vertex u, vertex v): edge• Create an edge between u and v. In a directed graph,

the edge will flow from u to v.• get-edges(vertex v): edge-set• Returns the set of edges flowing from v• get-neighbors(vertex v): vertex-set• Returns the set of vertexes connected to v

6

• An undirected graph.• In a directed graph, the

edges point from one vertex to another, while in an undirected graph, they merely connect two vertices.

Undirected Graphs

Application 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

7

Many geometrical objects such as cubes, polyhedral, 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. For example consider the cube:

8

Application Of Graphs

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.

9

Application Of Graphs

Neighborhood Graphs

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.

Application Of GraphsVoronoi Diagrams

In computational geometry the voronoi diagram is the main tool for storing clouds of points and for manipulating points in the Euclidean space. To create a voronoi diagram we must partition the graphical space into regions. This is done by separating every pair of data points which are nearest neighbors by a line which is centered between the points and perpendicular to an imaginary edge between them.

Representation of Graphs

Adjacency Matrix Representation

• An adjacency matrix is one of the two common ways to represent a graph. • The adjacency matrix shows which nodes are adjacent to one another. Two nodes are adjacent if there is an edge connecting them. • In the case of a directed graph, if node j is adjacent to node i, there is an edge from i to j. In other words, if j is adjacent to i, you can get from i to j by traversing one edge. For a given graph with n nodes, the adjacency matrix will have dimensions of n x n. • For an unweighted graph, the adjacency matrix will be populated with boolean values.

12

Representation of Graphs

Adjacency Matrix• For any given node i, you can determine its

adjacent nodes by looking at row of the adjacency matrix. A value of true at indicates that there is an edge from node i to node j, and false indicating no edge. In an undirected graph, the values of and will be equal.

• In a weighted graph, the boolean values will be replaced by the weight of the edge connecting the two nodes, with a special value that indicates the absence of an edge.

• The memory use of an adjacency matrix is O(n2).

13

Representation of GraphsAdjacency List Representation• One way to have the graph maintain a list of lists, in which the first list is a list of indices corresponding to each node in the graph. • Each of these refer to another list that stores a the index of each adjacent node to this one. It also useful to associate the weight of each link with the adjacent node in this list.Example: An undirected graph contains four nodes 1, 2, 3 and 4. 1 is linked to 2 and 3. 2 is linked to 3. 3 is linked to 4.1 - [2, 3]2 - [1, 3]3 - [1, 2, 4]4 - [3]

Representation of Graphs

Adjacency List Representation• It useful to store the list of all the nodes in the

graph in a hash table. The keys then would correspond to the indices of each node and the value would be a reference to the list of adjacent node indecies.

• Another implementation might require that each node keep a list of its adjacent nodes.

14

15

Graph Traversal

Depth-First Traversel• Start at vertex v, visit its neighbour w, then w's neighbour y and keep going

until reach 'a dead end' then iterate back and visit nodes reachable from second last visited vertex and keep applying the same principle.

• General algorithm:for each vertex v in the graph

if v is not visited start the depth first traversal at v

16

Depth-First TraversalExample the following graph:

Graph Traversal

0 1 2 4

5

6

3

7

8

9

10

Directed graph G3

17

Depth-First TraversalThe depth first ordering of the vertices graph G3:0 1 2 3 4 5 6 8 10 7 9The general algorithm to do a depth first traversal at a

givennode v is:1. Mark node v as visited2. Visit the node3. For each vertex u adjacent to v if u is not visited start the depth first traversal at u

Graph Traversal

18

Breadth-First Search Breadth first search visits the nodes neighbours and then the univisited neighbours of the neighbours etc. If it starts on vertex a it will go to all vertices that have an edge from a. If some points are not reachable it will have to start another BFS from a new vertex.

Graph Traversal

19

Breadth-First Search Breadth first search visits the nodes neighbours and then the univisited neighbours of the neighbours etc. If it starts on vertex a it will go to all vertices that have an edge from a. If some points are not reachable it will have to start another BFS from a new vertex.

Base on the same example in Depth-First Traversal :0 1 2 3 4 5 6 8 10 7 9 Start the traversal at vertex 0. After visiting vertex 0 next visit the

vertices that are directly connected to it, that are 1 and 5. Next visit the vertices that are directed connected to 1 and are not visit, that is 2 and 3. Then visit vertices that directly connected to 5 and are not visited, that is 6. Then continue to next path.

Graph Traversal

20

Breadth-First Search The general algorithm:

a. for each vertex v in the graph if v is not visited add v to the queue // start the breadth first search at vb. Mark v as visitedc. While the queue is not empty c.1. Remove vertex u from the queue c.2. Retrieve the vertices adjacent to u c.3. for each vertex w that is adjacent to u if w is not visited c.3.1. Add w to the queue c.3.2. Mark w as visited

Graph Traversal

21

Weighted Graphs

• We may also want to associate some cost or weight to the traversal of an edge. When we add this information, the graph is called weighted. An example of a weighted graph would be the distance between the capitals of a set of countries.

• Directed and undirected graphs may both be weighted. The operations on a weighted graph are the same with addition of a weight parameter during edge creation:

• Weighted Graph Operations (an extension of undirected/directed graph operations)

• make-edge(vertex u, vertex v, weight w): edge• Create an edge between u and v with weight w. In a

directed graph, the edge will flow from u to v.• Weight graph also being used to find the shortest path.

22

Shortest PathGeneral algorithm of Short Path:1. Initialize the array smallestWeight so that smallestWeight[u] = weights[vertex, u].2. Set samllestWeight [vertex] = 0.3. Find the vertex, v, that is closet to vertex for which the

shortest path has not been determined.4. Mark v as the (next) vertex for which the smallest

weight is found5. For each vertex w in G, such that the shortest path from

vertex to w has not been determined and an edge(v,w) exists, if the weight of the path to w via v is smaller than its current weight, update the weight of w to be the weight of v + the weight of edge (v,w).

23

Shortest PathWarshall’s Algorithm: A directed graph G with M nodes is maintained in memory by its adjacency matrix A. this algorithm find the (Boolean) path matrix P of the graph G.1.Repeat for I, J = 1,2…..,M:[Initialize P] if A[I,J] = 0 , then: Set P[I,J]:=0; Else: Set P[I,J]:=1. [End of loop]2. Repeat Steps 3 and 4 for I = 1,2,….,M:[Updates P]3. Repeat Step 4 for I = 1,2……,M:4. Repeat for J = 1,2,……,M: Set P[I,J]: =P[I,J] v (P[I,K]^ P[k,J]) [End of loop] [End of Step 3 loop] [End of Step 2 loop]5. Exit.

Minimum Spanning Tree

24

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; for instance the complete graph on four vertices o---o |\ / | | X | |/ \ | o---o

25

has sixteen spanning trees:

Minimum Spanning Tree

26

Now suppose the edges of the graph have weights or lengths. The weight of a tree is just the sum of weights of its edges. Obviously, different trees have different lengths. The problem: how to find the minimum length spanning tree?

This problem can be solved by many different algorithms. It is the topic of some very recent research. There are several "best" algorithms, depending on the assumptions :

• A randomized algorithm can solve it in linear expected time. [Karger, Klein, and Tarjan, "A randomized linear-time algorithm to find minimum spanning trees", J. ACM, vol. 42, 1995, pp. 321-328.]

Minimum Spanning Tree

• It can be solved in linear worst case time if the weights are small integers. [Fredman and Willard, "Trans-dichotomous algorithms for minimum spanning trees and shortest paths", 31st IEEE Symp. Foundations of Comp. Sci., 1990, pp. 719--725.]

• Otherwise, the best solution is very close to linear but not exactly linear. The exact bound is O(m log beta(m,n)) where the beta function has a complicated definition: the smallest i such that log(log(log(...log(n)...))) is less than m/n, where the logs are nested i times. [Gabow, Galil, Spencer, and Tarjan, Efficient algorithms for finding minimum spanning trees in undirected and directed graphs. Combinatorica, vol. 6, 1986, pp. 109--122.]

27

Minimum Spanning Tree

Why minimum spanning trees?• The standard application is to a problem like phone

network design. You want a set of lines that connects all your offices with a minimum total cost. If a network isn't a tree it can always remove some edges and save money.

• A less obvious application is that the minimum spanning tree can be used to approximately solve the traveling salesman problem. A convenient formal way of defining this problem is to find the shortest path that visits each point at least once.

• It's help in minimization over a strictly larger set. Example,. If draw a path tracing around the minimum spanning tree, trace that each edge twice and visit all points that is why MST weight is less than the TSP weight

28

Proof: Suppose you have a tree T not containing e; then I want to show that T is not the MST. Let e=(u,v), with u in X and v not in X. Then because T is a spanning tree it contains a unique path from u to v, which together with e forms a cycle in G. This path has to include another edge f connecting X to G-X. T+e-f is another spanning tree (it has the same number of edges, and remains connected since you can replace any path containing f by one going the other way around the cycle). It has smaller weight than t since e has smaller weight than f. So T was not minimum, which is what we wanted to prove.

29

How to find minimum spanning tree?

Kruskal's algorithm• We'll start with Kruskal's algorithm, which is easiest to

understand and probably the best one for solving problems by hand.

• Kruskal's algorithm: sort the edges of G in increasing order by length keep a subgraph S of G, initially empty for each edge e in sorted order if the endpoints of e are disconnected in S add e to S return S

Note that, whenever you add an edge (u,v), it's always the smallest connecting the part of S reachable from u with the rest of G, so by the lemma it must be part of the MST.

30

This algorithm is known as a greedy algorithm, because it chooses at each step the cheapest edge to add to S. E.g. if you want to find a shortest path from a to b, it might be a bad idea to keep taking the shortest edges. The greedy idea only works in Kruskal's algorithm because of the key property we proved.

31

Kruskal's algorithm

Analysis: The line testing whether two endpoints are disconnected looks like it should be slow (linear time per iteration, or O(mn) total). But actually there are some complicated data structures that let us perform each test in close to constant time; this is known as the union-find problem. The slowest part turns out to be the sorting step, which takes O(m log n) time.

32

Kruskal's algorithm

Prim's algorithm• Rather than build a subgraph one edge at a time, Prim's

algorithm builds a tree one vertex at a time. • Prim's algorithm:

let T be a single vertex x while (T has fewer than n vertices) {

find the smallest edge connecting T to G-T add it to T

} Since each edge added is the smallest connecting T to G-T, the lemma we proved shows that we only add edges that should be part of the MST.

33

34

Prim's algorithmPrim with heaps: make a heap of values (vertex,edge,weight(edge)) initially (v,-,infinity) for each vertex let tree T be empty while (T has fewer than n vertices) { let (v,e,weight(e)) have the smallest weight in the heap remove (v,e,weight(e)) from the heap add v and e to T for each edge f=(u,v) if u is not already in T find value (u,g,weight(g)) in heap if weight(f) < weight(g) replace (u,g,weight(g)) with (u,f,weight(f)) }

Analysis: We perform n steps in which we remove the smallest element in the heap, and at most 2m steps in which we examine an edge f=(u,v). For each of those steps, we might replace a value on the heap, reducing it's weight. (find the right value on the heap, but that can be done easily enough by keeping a pointer from the vertices to the corresponding values.) It's easy to do in O(log n) time. Alternately by using a more complicated data structure known as a Fibonacci heap, it can reduce the weight of an element in constant time. The result is a total time bound of O(m + n log n).

35

Prim's algorithm