Download presentation
Presentation is loading. Please wait.
Published byJeremy Parsons Modified over 9 years ago
1
Graphs Slide credits: K. Wayne, Princeton U. C. E. Leiserson and E. Demaine, MIT K. Birman, Cornell U.
2
These are not Graphs...not the kind we mean, anyway
3
These are Graphs K5K5 K 3,3 =
4
Applications of Graphs Communication networks Routing and shortest path problems Commodity distribution (flow) Traffic control Resource allocation Geometric modeling ...
5
Undirected Graphs Undirected graph. G = (V, E) is an ordered pair consisting of n V = nodes. n E = edges between pairs of nodes. n Captures pairwise relationship between objects. n Graph size parameters: n = |V|, m = |E|. n Two nodes connected by an edge are said to be neighbor. 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 = 8 m = 11
7
Some Properties of Undirected Graph n |E|= O(V 2 ) n If G is connected |E|≥|V|–1 – An undirected graph is connected if for every pair of nodes u and v, there is a path between u and v. n deg(V) is defined as the number of edges incident to V – Handshaking lemma: Σ v ∈ V deg(v) = 2|E|
8
Directed Graphs Directed graph. G = (V, E) n Edge (u, v) goes from node u to node v. Ex. Web graph - hyperlink points from one web page to another. n Directedness of graph is crucial. n Modern web search engines exploit hyperlink structure to rank web pages by importance.
9
World Wide Web Web graph. n Node: web page. n Edge: hyperlink from one page to another. cnn.com cnnsi.com novell.comnetscape.com timewarner.com hbo.com sorpranos.com
10
Graph Representation: Adjacency Matrix Adjacency matrix. n-by-n matrix with A uv = 1 if (u, v) is an edge. n Two representations of each edge. n Space proportional to n 2. n Checking if (u, v) is an edge takes O(1) time. n Identifying all edges takes O(n 2 ) time. n Use for dense graph 1 2 3 4 5 6 7 8 1 0 1 1 0 0 0 0 0 2 1 0 1 1 1 0 0 0 3 1 1 0 0 1 0 1 1 4 0 1 0 1 1 0 0 0 5 0 1 1 1 0 1 0 0 6 0 0 0 0 1 0 0 0 7 0 0 1 0 0 0 0 1 8 0 0 1 0 0 0 1 0
11
Graph Representation: Adjacency List Adjacency list. Node indexed array of lists. n Two representations of each edge. n Space proportional to m + n. n Checking if (u, v) is an edge takes O(deg(u)) time. n Identifying all edges takes O(m + n) time. n Use for sparse graph 1 23 2 3 4 25 5 6 7 38 8 1345 125 8 7 2346 5 degree = number of neighbors of u 37
12
Paths and Connectivity Def. A path in an undirected graph G = (V, E) is a sequence P of nodes v 1, v 2, …, v k-1, v k with the property that each consecutive pair v i, v i+1 is 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.
13
Cycles Def. A cycle is a path v 1, v 2, …, v k-1, v k in which v 1 = v k, k > 2, and the first k-1 nodes are all distinct. cycle C = 1-2-4-5-3-1
14
Trees Def. 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. n G is connected. n G does not contain a cycle. n G has n-1 edges.
15
Rooted Trees Rooted tree. Given a tree T, choose a root node r and orient each edge away from r. Importance. Models hierarchical structure. a tree the same tree, rooted at 1 v parent of v child of v root r
16
Graph Traversal n Problem: Search for a certain node or traverse all nodes in the graph n Depth First Search – Once a possible path is found, continue the search until the end of the path n Breadth First Search – Start several paths at a time, and advance in each one step at a time
17
Depth First Traversal n A natural way to do Depth-first search (BFS) is using recursion. DFS( Node c ) { Mark c "Visited” For each neighbor n of c If n "Unvisited" DFS( n ) } Possible visit sequence: 1, 2, 4, 5, 6, 3, 8, 7
18
Breadth First Traversal n Breadth-first search (BFS) not naturally recursive. n Use a queue so that vertices are visited in order according to their distance from the starting vertex. BFS(Node v) { create a queue Q enqueue v onto Q mark v “Visited” while Q is not empty { dequeue t from Q for each neighbor u of t do if u is not marked { mark u “Visited” enqueue u onto Q }
19
Breadth First Search L0L0 L1L1 L2L2 L3L3 Visit sequence: 1, 2, 3, 4, 5, 7, 8, 6
20
Applications: Finding a Path n Find path from source vertex s to destination vertex d n Use graph search starting at s and terminating as soon as we reach d n Need to remember edges traversed n Use depth – first search
21
DFS E F G B C D A start destination A DFS on A A DFS on B B A DFS on C B C A B Return to call on B D Call DFS on D A B D Call DFS on G G found destination - done! Path is implicitly stored in DFS recursion Path is: A, B, D, G DFS Process
22
Minimum spanning trees Minimum spanning tree. Given a connected undirected graph G = (V, E) with real-valued edge weights c e, an MST is a subset of the edges T E such that T is a spanning tree whose sum of edge weights is minimized.
23
Minimum spanning trees
24
Greedy Algorithms Prim's algorithm. Start with some root node s and greedily grow a tree T from s outward. At each step, add the cheapest edge e to T that has exactly one endpoint in T. Theorem. Let T be the MST of G= (V, E), and let A ⊆ V. Suppose that (u, v) ∈ E is the least-weight edge connecting A to V–A. Then, (u, v) ∈ T.
25
Greedy Algorithms Proof: Suppose (u, v) ∉ T. Consider the unique simple path from u to v in T. Swap (u, v) with the first edge on this path that connects a vertex in A to a vertex in V–A to get a lower-weight MST
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.