graphs
play

graphs Nov. 13, 2017 1 Example e g a c f d h b 2 Same - PowerPoint PPT Presentation

COMP 250 Lecture 28 graphs Nov. 13, 2017 1 Example e g a c f d h b 2 Same Example different notation e g a c f d h b 3 Weighted Graph e g 9 a 12 11 4 c f 5 7 7 d 2 4 h b 4 Definition A directed graph is


  1. COMP 250 Lecture 28 graphs Nov. 13, 2017 1

  2. Example e g a c f d h b 2

  3. Same Example – different notation e g a c f d h b 3

  4. Weighted Graph e g 9 a 12 11 4 c f 5 7 7 d 2 4 h b 4

  5. Definition A directed graph is a set of vertices 𝑊 = {𝑤 𝑗 ∶ 𝑗 ∈ 1, … , 𝑜 } and set of ordered pairs of these vertices called edges . 𝐹 = { 𝑤 𝑗 , 𝑤 𝑘 ∶ 𝑗, 𝑘 ∈ 1, … , 𝑜 } In an undirected graph, the edges are unordered pairs. 𝐹 = 𝑤 𝑗 , 𝑤 𝑘 ∶ 𝑗, 𝑘 ∈ 1, … , 𝑜

  6. Examples Vertices Edges airports web pages Java objects 6

  7. Examples Vertices Edges airports flights web pages Java objects 7

  8. Examples Vertices Edges airports flights web pages links (URLs) Java objects 8

  9. Examples Vertices Edges airports flights web pages links (URLs) Java objects references 9

  10. linked trees lists graphs

  11. Terminology: “in degree” e a v in degree a 1 c f b 2 d c 2 b g d 0 e 1 f 3 g 0 h h 1 11

  12. Terminology: “out degree” e a v out degree a 1 c f b 1 d c 1 b g d 2 e 2 f 2 g 1 h h 0 12

  13. Example: web pages e a In degree: How many web pages link to some web page c f (e.g. f ) ? d b g Out degree: How many web pages does some web page (e.g. f ) link to ? h 13

  14. Terminology: path A path is a sequence of e a edges such that end vertex of one edge is c f the start vertex of the next edge and no d b vertex repeated except g maybe first and last. Examples • acfeb • h dac • febf • ….. 14

  15. Graph algorithms in COMP 251 Given a graph, what is the shortest (weighted) path between two vertices?

  16. Terminology: cycle A cycle is a path such e a that the last vertex is the same as the first c f vertex. d b g Examples • febf • efe • fbf h • … 16

  17. “Travelling Salesman” COMP 360 (Hamiltonian circuit) 11 a c Find the shortest cycle 12 that visits all vertices once. 7 2 7 How many potential cycles are there in a d g graph of n vertices ? 4

  18. Directed Acyclic Graph no cycles Used to capture dependencies. b a There are three paths d from a to d. c

  19. 202 Intro Program MATH (prereqs for many upper level COMP courses) 240 223 222 323 206 250 Disc. Linear Cal III Prob. Software Intro Sys Str. 1 Alg. CompSci 273 303 302 251 350 Comp. Software Program Data Str Num. Sys. Design Lang & Alg Meth 310 421 360 330 424 Oper. Data- Alg. Theory Artif. Sys. bases Design Comp. Intel. APPLICATIONS THEORY SYSTEMS (graphics, vision, (crypto, optimization, game theory, (compilers, networks, distributed sys, bioinf, games, logic, correctness, computability..) 19 concurrency, web,..) machine learning..)

  20. Graph ADT • addVertex( … ), addEdge (…) • containsVertex ( …), containsEdge (… ) • getVertex ( … ), getEdge ( … ) • removeVertex ( …), removeEdge ( …) • numVertices( ), numEdges( ) • … How to implement a Graph class? A graph is a generalization of a tree, so …

  21. Recall: How to implement a rooted tree in Java ? class Tree<T>{ TreeNode<T> root; : // alternatively…. // inner class class TreeNode<T>{ class TreeNode<T>{ T element; T element; TreeNode<T> firstChild; ArrayList< TreeNode<T> > children; TreeNode<T> nextSibling; TreeNode<T> parent; : } } } 21

  22. Adjacency List (generalization of children for graphs) e a v v.adjList a c c f b f d c f b g d a, c e b, f f b, e g h h h Here each adjacency list is sorted, but that is not always possible (or necessary). 22

  23. How to implement a Graph class in Java? class Graph<T> { class Vertex<T> // Could have called it GNode { ArrayList<Vertex> adjList; T element; } } This is a very basic Graph class.

  24. How to implement a Graph class in Java? class Graph<T> { class Vertex<T> { ArrayList<Edge> adjList; T element; boolean visited; } class Edge { Vertex endVertex; double weight; : } } Unlike a rooted tree, there is no notion of a root vertex in a graph.

  25. How to reference vertices? Suppose we have a string name (key) for each vertex. e.g. YUL for Trudeau airport, LAX for Los Angeles, … class Graph<T> { HashMap< String, Vertex<T> > vertexMap; : class Vertex<T> { …} class Edge<T> { …} } We could also just enumerate vertices. 25

  26. How many objects ? e f b 26

  27. e Graph HashMap f b Vertex Vertex Vertex Edge Edge Edge Edge Edge ArrayList ArrayList ArrayList 27

  28. Adjacency Matrix a b c d e f e a a 0 0 1 0 0 0 b 0 0 0 0 0 1 c f c 0 0 0 0 0 1 d 1 0 1 0 0 0 d b e 0 1 0 0 0 1 f 0 1 0 0 1 0 boolean adjMatrix[ 6 ][ 6 ] Assume we have a mapping from vertex names to 0, 1, …. , n -1. 28

  29. Adjacency Matrix loop a b c d e f e a a 1 0 1 0 0 0 b 0 0 0 0 0 1 c f c 0 0 0 0 0 1 d 1 0 1 0 0 0 d b e 0 1 0 0 1 1 f 0 1 0 0 1 0 boolean adjMatrix[ 6 ][ 6 ] 29

  30. Suppose a graph has 𝑜 vertices. The graph is dense if number of edges is close to 𝑜 2 . The graph is sparse if number of edges is close to 𝑜. (These are not formal definitions.)

  31. Exercise Would you use an adjacency list or adjacency matrix for each of the following? • The graph is sparse e.g. 10,000 vertices and 20,000 edges and we want to use as little space as possible. • The graph has 10,000 vertices and 20,000,000 edges, and it is important to use as little space as possible. • You need to answer the query areAdjacent() as quickly as possible, no matter how much space you use. • You need to perform operation insertVertex. • You need to perform operation removeVertex.

  32. Exercise Would you use an adjacency list or adjacency matrix for each of the following? • The graph is sparse e.g. 10,000 vertices and 20,000 edges and we want to use as little space as possible. • The graph is dense e.g. 10,000 vertices and 20,000,000 edges, and we want to use as little space as possible. • You need to answer the query areAdjacent() as quickly as possible, no matter how much space you use. • You need to perform operation insertVertex. • You need to perform operation removeVertex.

  33. Exercise Would you use an adjacency list or adjacency matrix for each of the following? • The graph is sparse e.g. 10,000 vertices and 20,000 edges and we want to use as little space as possible. • The graph is dense e.g. 10,000 vertices and 20,000,000 edges, and we want to use as little space as possible. . • Answer the query areAdjacent() as quickly as possible, no matter how much space you use. • You need to perform operation insertVertex. • You need to perform operation removeVertex.

  34. Exercise Would you use an adjacency list or adjacency matrix for each of the following? • The graph is sparse e.g. 10,000 vertices and 20,000 edges and we want to use as little space as possible. • The graph is dense e.g. 10,000 vertices and 20,000,000 edges, and we want to use as little space as possible. . • Answer the query areAdjacent() as quickly as possible, no matter how much space you use. • Perform operation insertVertex( v ). • You need to perform operation removeVertex.

  35. Exercise Would you use an adjacency list or adjacency matrix for each of the following? • The graph is sparse e.g. 10,000 vertices and 20,000 edges and we want to use as little space as possible. • The graph is dense e.g. 10,000 vertices and 20,000,000 edges, and we want to use as little space as possible. . • Answer the query areAdjacent() as quickly as possible, no matter how much space you use. • Perform operation insertVertex( v ). • Perform operation removeVertex( v ).

  36. Next lecture • Recursive graph traversal • depth first • Non-recursive graph traversal • depth first • breadth first 36

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend