Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)

Similar presentations


Presentation on theme: "Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)"— Presentation transcript:

1 Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)

2 Graph 2

3 Graph Köenigsberg bridge problem 3

4 Graph Definitions A graph G consists of two sets –a finite, nonempty set of vertices V(G) –a finite, possible empty set of edges E(G) –G(V,E) represents a graph An undirected graph is one in which the pair of vertices in an edge is unordered –(v 0,v 1 ) = (v 1,v 0 ) A directed graph is one in which each edge is a directed pair of vertices – ≠ 4

5 Graph Examples complete undirected graph: n(n-1)/2 edges complete directed graph: n(n-1) edges 5 G1G1 complete graph incomplete graph V(G 1 )={0,1,2,3}E(G 1 )={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)} V(G 2 )={0,1,2,3,4,5,6}E(G 2 )={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)} V(G 3 )={0,1,2}E(G 3 )={,, } 0 12 3 0 12 3 0 12 3456 G2G2 0 1 2 G3G3

6 Graph Restrictions A graph may not have an edge from a vertex, i, back to itself –such edges are known as self loops A graph may not have multiple occurrences of the same edge –if we remove this restriction, we obtain a data referred to as a multi-graph 6

7 feedback loops 7 multi-graph

8 Graph Adjacent and incident If (v 0,v 1 ) is an edge in an undirected graph –v 0 and v 1 are adjacent –the edge (v 0,v 1 ) is incident on v 0 and v 1 If is an edge in a directed graph –v 0 is adjacent to v 1, and v 1 is adjacent from v 0 –the edge is incident on v 0 and v 1 8 01 01

9 Graph Sub-graph A sub-graph of G is a graph G’ such that V(G’)  V(G) and E(G’)  E(G) 9

10 10 G1G1 0 12 3 0 1 2 G3G3

11 Graph Path A path from vertex v p to vertex v q in a graph G, is a sequence of vertices, v p, v i1, v i2,..., v in, v q, such that (v p,v i1 ), (v i1,v i2 ),..., (v in,v q ) are edges in G –a path such as (0,2), (2,1), (1,3) is also written as 0,2,1,3 The length of a path is the number of edges on it 11 0 12 3 0 12 3 0 12 3

12 Graph Simple path and cycle Simple path (simple directed path) –a path in which all vertices, except possibly the first and the last, are distinct A cycle is a simple path in which the first and the last vertices are the same 12 0 12 3

13 Graph Connected graph/component Connected graph –in an undirected graph G, two vertices, v 0 and v 1, are connected if there is a path in G from v 0 to v 1 –an undirected graph is connected if, for every pair of distinct vertices v i, v j, there is a path from v i to v j Connected component –a connected component of an undirected graph is a maximal connected sub-graph by maximal, no other sub-graph that is both connected and properly contains the component A tree is a graph that is connected and acyclic (i.e., has no cycle) 13

14 14

15 Connected component Strongly connected A directed graph is strongly connected if there is a directed path from v i to v j and also from v j to v i A strongly connected component is a maximal sub-graph that is strongly connected 15

16 Any Questions? 16

17 How Many 17 Strongly connected components in this graph? 0 1 2 G3G3

18 not strongly connected 2 strongly connected components (maximal strongly connected sub-graph) 18 0 1 2 G3G3 0 1 2

19 Graph Degree The degree of a vertex is the number of edges incident to that vertex For directed graph –in-degree(v): the number of edges that have v as the head –out-degree(v): the number of edges that have v as the tail If d i is the degree of a vertex i in a graph G with n vertices and e edges, the number of edges is – 19

20 20 undirected graph directed graph 0 12 3 3 3 3 3 0 12 3456 2 33 1111 in:1, out:1 in:1, out:2 in:1, out:0 0 1 2

21 21

22 Graph Representations Adjacency matrix Adjacency lists Adjacency multi-lists 22

23 Graph representation Adjacency matrix 23

24 Graph representation Adjacency list 24

25 Graph representation Adjacency multi-list 25

26 Adjacency matrix Let G = (V,E) be a graph with n vertices The adjacency matrix of G is a two- dimensional n x n array, say adj_mat If the edge (v i,v j ) is in E(G), then adj_mat[i][j]=1, otherwise adj_mat[i][j]=0 The adjacency matrix for an undirected graph is symmetric; the adjacency matrix for a digraph need not be symmetric 26

27 27 G1G1 0 12 3 0 1 2 G3G3 G4G4 0 12 3 4 5 7 6

28 Adjacency matrix Merits For an undirected graph, the degree of any vertex, i, is its row sum: For a directed graph, the row sum is the out-degree, while the column sum is the in-degree The complexity of checking edge number or examining if G is connect –G is undirected: O(n 2 /2) –G is directed: O(n 2 ) 28

29 Adjacency Lists There is one list for each vertex in G –the nodes in list i represent the vertices that are adjacent from vertex i For an undirected graph with n vertices and e edges, this representation requires n head nodes and 2e list nodes 29

30 G1G1 30 G1G1 0 12 3 0 1 2 G3G3 G4G4 0 12 3 4 5 7 6

31 Any Questions? 31

32 Adjacency Lists Interesting operations Degree of a vertex in an undirected graph –# of nodes in its adjacency list # of edges in a graph –determined in O(n+e) Out-degree of a vertex in a directed graph –# of nodes in its adjacency list In-degree of a vertex in a directed graph –traverse the whole data structure 32 answer

33 Adjacency list Sequential Representation Sequentially pack the nodes on the adjacency list n vertices and e edges requires an array of n+2e+2 nodes –node[0] … node[n-1]: gives the starting point of the list for vertex i –node[n]: n+2e+1 –node[n+1] … node[n+2e]: the other end of the edge The vertices adjacent from vertex I are stored in node[node[i]] … node[node[i+1]-1], 0 ≦ i < n 33 [0]9[13]02 [1]11[14]3 [2]13[15]13 [3]15[16]2 [4]17[17]54 [5]18[18]45 [6]20[19]6 [7]22[20]56 [8]23[21]7 [9]10[22]67 [10]2 [11]01 [12]3

34 Adjacency list Inverse adjacency list Finding in-degree of vertices 34 0 1 2 G3G3 [0] [1] [2] 1NULL 0 1

35 Adjacency list Orthogonal representation 35 01NULL 01 01 01 01 01 01 01 01 0 1 2 G3G3

36 Adjacency list Order is of no significance 36 [0] 312NULL [1]203NULL [2]301NULL [3]210NULL G1G1 0 12 3

37 Any Questions? 37 About the previous representation

38 Is There 38 Any waste?

39 Adjacency multi-lists Lists in which nodes may be shared among several lists –an edge is shared by two different paths There is exactly one node for each edge 39

40 40 G1G1 0 12 3

41 Graph Weighted edges The edges have weights assigned to them These weights may represent as –the distance from one vertex to another –the cost of going from one vertex to an adjacent vertex Adjacency matrix –adj_mat[i][j] would keep the weights Adjacency lists –add a weight field to the node structure A graph with weighted edges is called a network 41

42 Graph Operations Traversal –given G=(V,E) and vertex v, find all wV, such that w connects v –Depth First Search (DFS) preorder traversal –Breadth First Search (BFS) level order traversal Spanning tree Biconnected components 42

43 Traversal An example 43 DFS: v 0,v 1,v 3,v 7,v 4,v 5,v 2,v 6 BFS: v 0,v 1,v 2,v 3,v 4,v 5,v 6,v 7

44 Depth First Search 44

45 Breadth First Search It needs a queue to implement breadth-first search –typedef struct queue *queue_pointer; typedef struct queue { int vertex; queue_pointer link; }; void addq(queue_pointer *, queue_pointer *, int); int deleteq(queue_pointer *); 45

46 46

47 Connected components If G is an undirected graph, then one can determine whether or not it is connected: –simply making a call to either dfs or bfs –then determining if there is any unvisited vertex Adjacency list: O(n+e) Adjacency matrix: O(n 2 ) 47

48 Spanning tree Definition: A tree T is said to be a spanning tree of a connected graph G if T is a subgraph of G and T contains all vertices of G E(G): T (tree edges) + N (nontree edges) –T: set of edges used during search –N: set of remaining edges 48

49 Spanning tree DFS/BFS When graph G is connected, a depth first or breadth first search starting at any vertex will visit all vertices in G We may use DFS or BFS to create a spanning tree –Depth first spanning tree when DFS is used –Breadth first spanning tree when BFS is used 49

50 Spanning tree DFS/BFS When graph G is connected, a depth first or breadth first search starting at any vertex will visit all vertices in G We may use DFS or BFS to create a spanning tree –depth first spanning tree when DFS is used –breadth first spanning tree when BFS is used 50

51 Spanning tree Properties If a nontree edge (v,w) is introduced into any spanning tree T, then a cycle is formed A spanning tree is a minimal subgraph, G’, of G such that V(G’) = V(G) and G’ is connected –we define a minimal sub-graph as one with the fewest number of edge –a spanning tree has n-1 edges 51

52 Biconnected graph (G is an undirected, connected graph) Definition: A vertex v of G is an articulation point iff the deletion of v, together with the deletion of all edges incident to v, leaves behind a graph that has at least two connected components Definition: A biconnected graph is a connected graph that has no articulation points Definition: A biconnected component of a connected graph G is a maximal biconnected subgraph H of G 52

53 Articulation point Examples of articulation points (node 1, 3, 5, 7) 53 0 1 23 4 5 6 7 890 1 23 4 5 6 7 890 1 23 4 5 6 7 89 connected graph two connected componentsone connected graph

54 Any Questions? 54

55 Biconnected component 55 answer

56 Biconnected component Using DFS By using depth first spanning tree of a connected undirected graph The depth first number (dfn) outside the vertices in the figures gives the DFS visit sequence If u is an ancestor of v then dfn(u) < dfn(v) 56 dfs(3): depth first spanning tree start from 3 0 1 2 3 45 6 7 89 0 1 2 3 4 5 6 7 89 0 1 23 4 5 6 7 89 0 1 2 3 4 5 6 7 89

57 Using DFS to find biconnected components dfn and low low(u) –the lowest dfn that we can reach from u using a path of descendants followed by at most one back edge –low(u) = min { dfn(u), min{ low(w) | w is a child of u }, min{ dfn(w) | (u,w) is a back edge } } 57 0 1 2 3 45 6 7 89 0 1 2 3 4 5 6 7 89 0 1 23 4 5 6 7 89 0 1 2 3 4 5 6 7 89 nontree edge (back edge)

58 Using DFS to find biconnected components An example dfn and low values for dfs spanning tree with root = 3 –low(u) = min { dfn(u), min{ low(w) | w is a child of u }, min{ dfn(w) | (u,w) is a back edge } } 58 Vdfnlowchildlow_childlow:dfncut 044(4,n,n)null null:4 130(3,4,0)04 4≧34≧3 * 220(2,0,n)100<2 300(0,0,n)4,50,5 0,5 ≧ 0 * 410(1,0,n)200<1 555(5,5,n)65 5≧55≧5 * 665(6,5,n)755<6 775(7,8,5)8,99,8 9,8 ≧ 7 * 899(9,n,n)null null:9 988(8.n,n)null null:8 0 1 2 3 45 6 7 89 0 1 2 3 4 5 6 7 89

59 Using DFS to find biconnected components Articulation point Any vertex u is an articulation point iff –u is the root of the spanning tree and has two or more children –u is not the root and has at least one child w such that we cannot reach an ancestor of u using a path that consists of only w descendants of w a single back edge –low(w)  dfn(u) 59 0 1 2 3 45 6 7 89 0 1 2 3 4 5 6 7 89 vertex0123456789 dfn4320156798 low4000055598

60 Using DFS to find biconnected components Determining dfn and low 60

61 Using DFS to find biconnected components The biconnected components Partition the edges of the connected graph into their biconnected components –in dfnlow(), we know that low[w] has been computed following the return from the function call dfnlow(w,u) –if low[w]  dfn[u], then we have identified a new biconnected component –we can output all edges in a biconnected component if we use a stack to save the edges when we first encounter them –the function bicon() contains the code modified from dfnlow() 61

62 62

63 Any Questions? 63

64 AvA Network 64 Inlist of pairs and a minimum size, n OutAvA networks Requirement - all vs. all network - complexity/teamwork report - using C would be the best Bonus - allow some missing edges (most vs. most)

65 Deadline 65 2010/4/20 23:59 Zip your code, step-by-step README, complexity analyses and anything worthy extra credit. Email to darby@ee.ncku.edu.tw. darby@ee.ncku.edu.tw

66 Graph Algorithms and applications Shortest path –4  5: 4,1,3,5 –all pair shortest path Topological order –a,b,c,e,d –O(n+e)–O(n+e) Bipartite –maximum matching weighted or unweighted O(n 2 +ne) –all vs. all network are similar to bipartite except that the edges between vertices of the same group are allowed an n vs. n network is formed by (n–1) vs. (n–1) networks 66 2 14 35 2 4 1 1 1 31 1 5 bad ce

67 Minimum cost spanning tree The cost of a spanning tree of a weighted, undirected graph is the sum of the costs (weights) of the edges in the spanning tree A minimum-cost spanning tree is a spanning tree of least cost Three different algorithms can be used to obtain a minimum cost spanning tree –Kruskal’s algorithm –Prim’s algorithm –Sollin’s algorithm All the three use a design strategy called the greedy method 67

68 Minimum cost spanning tree Greedy strategy Construct an optimal solution in stages At each stage, we make the best decision (selection) possible at this time –using least-cost criterion for constructing minimum-cost spanning trees Make sure that the decision will result in a feasible solution, where a feasible solution works within the constraints specified by the problem –our solution must satisfy the following constrains must use only edges within the graph must use exactly n – 1 edges may not use edges that produce a cycle 68

69 Minimum cost spanning tree Kruskal’s algorithm Build a minimum cost spanning tree T by adding edges to T one at a time Select the edges for inclusion in T in non- decreasing order of the cost An edge is added to T if it does not form a cycle Since G is connected and has n > 0 vertices, exactly n – 1 edges will be selected Time complexity: O(eloge) 69

70 70

71 Minimum cost spanning tree Prim’s algorithm Build a minimum cost spanning tree T by adding edges to T one at a time At each stage, add a least cost edge to T such that the set of selected edges is still a tree Repeat the edge addition step until T contains n – 1 edges 71

72 72

73 Minimum cost spanning tree Sollin’s algorithm Selects several edges for inclusion in T at each stage At the start of a stage, the selected edges forms a spanning forest During a stage we select a minimum cost edge that has exactly one vertex in the tree edge for each tree in the forest Repeat until only one tree at the end of a stage or no edges remain for selection 73


Download ppt "Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)"

Similar presentations


Ads by Google