算法设计与分析算法设计与分析 (4).ppt





《算法设计与分析算法设计与分析 (4).ppt》由会员分享,可在线阅读,更多相关《算法设计与分析算法设计与分析 (4).ppt(34页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、1Chapter 3GraphsSlides by Kevin Wayne.Copyright 2005 Pearson-Addison Wesley.All rights reserved.3.1 Basic Definitions and Applications3Undirected GraphsUndirected graph.G=(V,E)nV=nodes.nE=edges between pairs of nodes.nCaptures pairwise relationship between objects.nGraph size parameters:n=|V|,m=|E|.
2、V=1,2,3,4,5,6,7,8 E=1-2,1-3,2-3,2-4,2-5,3-5,3-7,3-8,4-5,5-6 n=8m=114Some Graph ApplicationstransportationGraphstreet intersectionsNodesEdgeshighwayscommunicationcomputersfiber optic cablesWorld Wide Webweb pageshyperlinkssocialpeoplerelationshipsfood webspeciespredator-preysoftware systemsfunctionsf
3、unction callsschedulingtasksprecedence constraintscircuitsgateswires5Graph Representation:Adjacency MatrixAdjacency matrix.n-by-n matrix with Auv=1 if(u,v)is an edge.nTwo representations of each edge.nSpace proportional to n2.nChecking if(u,v)is an edge takes(1)time.nIdentifying all edges takes(n2)t
4、ime.1 2 3 4 5 6 7 81 0 1 1 0 0 0 0 02 1 0 1 1 1 0 0 03 1 1 0 0 1 0 1 14 0 1 0 1 1 0 0 05 0 1 1 1 0 1 0 06 0 0 0 0 1 0 0 07 0 0 1 0 0 0 0 18 0 0 1 0 0 0 1 06Graph Representation:Adjacency ListAdjacency list.Node indexed array of lists.nTwo representations of each edge.nSpace proportional to m+n.nChec
5、king if(u,v)is an edge takes O(deg(u)time.nIdentifying all edges takes(m+n)time.1232342556738813451258723465degree=number of neighbors of u377Paths and ConnectivityDef.A path in an undirected graph G=(V,E)is a sequence P of nodes v1,v2,vk-1,vk with the property that each consecutive pair vi,vi+1 is
6、joined by an edge in E.Def.A path is simple if all nodes are distinct.Def.An undirected graph is connected if for every pair of nodes u and v,there is a path between u and v.8CyclesDef.A cycle is a path v1,v2,vk-1,vk in which v1=vk,k 2,and the first k-1 nodes are all distinct.cycle C=1-2-4-5-3-19Tre
7、esDef.An undirected graph is a tree if it is connected and does not contain a cycle.Theorem.Let G be an undirected graph on n nodes.Any two of the following statements imply the third.nG is connected.nG does not contain a cycle.nG has n-1 edges.10Rooted TreesRooted tree.Given a tree T,choose a root
8、node r and orient each edge away from r.a treethe same tree,rooted at 1vparent of vchild of vroot r3.2 Graph Traversals-t connectivity problem.Given two node s and t,is there a path between s and t?s-t shortest path problem.Given two node s and t,what is the length of the shortest path between s and
9、 t?12Connectivity13Breadth First SearchExplore outward from s in all possible directions,adding nodes one layer at a time.BFS algorithm.nL0=s.nL1=all neighbors of L0.nL2=all nodes that do not belong to L0 or L1,and that have an edge to a node in L1.nLi+1=all nodes that do not belong to an earlier la
10、yer,and that have an edge to a node in Li.Theorem.For each i,Li consists of all nodes at distance exactly ifrom s.There is a path from s to t iff t appears in some layer.sL1L2L n-114Breadth First SearchProperty.Let T be a BFS tree of G=(V,E),and let(x,y)be an edge of G.Then the level of x and y diff
11、er by at most 1.L0L1L2L315Breadth First Search:AnalysisTheorem.The above implementation of BFS runs in O(m+n)time if the graph is given by its adjacency list representation.Pf.when we consider node u,there are deg(u)incident edges(u,v)total time processing edges is uV deg(u)=2m each edge(u,v)is coun
12、ted exactly twicein sum:once in deg(u)and once in deg(v)16Connected ComponentConnected component.Find all nodes reachable from s.Connected component containing node 1=1,2,3,4,5,6,7,8.3.4 Testing Bipartiteness18Bipartite GraphsDef.An undirected graph G=(V,E)is bipartite if the nodes can be colored re
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 算法设计与分析算法设计与分析 4 算法 设计 分析

限制150内