cs200 graphs
play

CS200: Graphs Prichard Ch. 14 Rosen Ch. 10 CS200 - Graphs 1 - PowerPoint PPT Presentation

CS200: Graphs Prichard Ch. 14 Rosen Ch. 10 CS200 - Graphs 1 Graphs A collection of What can this nodes and edges represent? n A computer network n Abstraction of a map n Social network CS200 - Graphs 2 Directed Graphs A


  1. CS200: Graphs Prichard Ch. 14 Rosen Ch. 10 CS200 - Graphs 1

  2. Graphs A collection of What can this nodes and edges represent? n A computer network n Abstraction of a map n Social network CS200 - Graphs 2

  3. Directed Graphs A collection of Sometimes we nodes and want to represent directed edges directionality: n Unidirectional network connections n One way streets n The web CS200 - Graphs 3

  4. Graphs/Networks Around Us http://lin-ear-th-inking.blogspot.com/2010/12/ visualizing-geodetic-information-with.html http://noduslabs.com/wp-content/uploads/2011/12/ figure-5-meaning-circulation.png CS200 - Graphs 4

  5. Graph Terminology Two vertices (or nodes) G=(V, E) are adjacent if they are connected by an edge. Vertices Edges An edge is incident on two vertices, an edge e can be represented by two Edges vertices (u,v) Degree of a vertex or node: number of edges incident on it u e Graph terminology: Vertices/ 14.1 in Prichard, Nodes 10.1 in Rosen v CS200 - Graphs 5

  6. Undirected Graphs n Undirected graph. G = (V, E) q V = set of nodes. q E = set of edges between pairs of nodes. q Captures pairwise relationship between objects. q Graph size parameters: n = |V|, m = |E|. 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 6

  7. Directed Graphs n Directed graph. G = (V, E) q Edge (u, v) goes from node u to node v. n Example. Web graph - hyperlink points from one web page to another. q Modern web search engines exploit hyperlink structure to rank web pages by importance (pageRank). 7

  8. Graph definitions Graph G = (V, E) , V: set of node s or vertices, E: set of edge s (pairs of nodes). In an undirected graph, edges are unordered pairs (sets) of nodes. In a directed graph edges are ordered pairs (2-tuples) of nodes. Path : sequence of nodes (v 0 ..v n ) s.t. ∀ i: (v i ,v i+1 ) is an edge. Path length : number of edges in the path, or sum of weights. Simple path: all nodes distinct. Cycle : path with first and last node equal. Acyclic graph : graph without cycles. DAG : directed acyclic graph. Two nodes are adjacen t if there is an edge between them. In a complete graph all nodes in the graph are adjacent.

  9. More definitions An undirected graph is connected if for all nodes v i and v j there is a path from v i to v j . G’(V’, E’) is a sub-graph of G(V,E) if V’ ⊆ V and E’ ⊆ E The sub-graph of G induced by V’ has all the edges (u,v) ∈ E such that u ∈ V’ and v ∈ V’. In a weighted graph the edges have a weight (cost, length,..) associated with them.

  10. Graph Terminology induced subgraph A subgraph of a graph G = (V,E) is a graph (V’,E’) such that V’ is a subset of V and, E’ is a subset of E The sub-graph of G induced by V’ has all the edges (u,v) ∈ E such that u ∈ V’ and v ∈ V’. CS200 - Graphs 10

  11. Question n A Tree is a subtype (special type) of Graph. A. True B. False CS200 - Graphs 11

  12. Paths n Path: a sequence of edges, e.g. ((v 1 ,v 2 ), (v 2 ,v 3 ), (v 3 ,v 4 )) s.t. the first node in the next edge is the second node in the v 1 e 1 previous edge. v 2 n A simple path passes through a vertex only once. n (e 1 , e 2 , e 3 ) is a simple path of v 4 e 2 length 3 from v 1 to v 4 n A path can be represented by e 3 a sequence of vertices, here v 3 (v 1 ,v 2 ,v 3 ,v 4 ) CS200 - Graphs 12

  13. Graph Terminology Self loop (loop): an edge that connects a vertex to itself (Simple) Graph: no self loops and no two edges connect the same vertices (E is a set, so no multiples). We are mostly thinking about these. Multigraph: may have multiple edges connecting the same vertices (not a graph: E is a set in graph ) Pseudograph: multigraph with self-loops CS200 - Graphs 13

  14. Complete Graphs n Simple graph that contains exactly one edge between each pair of distinct vertices. Complete Graph CS200 - Graphs 14

  15. Question Which describes this graph? c b A. Simple B. Pseudograph C. Cycle D. Complete e f CS200 - Graphs 15

  16. Question Which describes this graph? A. Simple c b B. Pseudograph C. Cycle D. Complete a e f CS200 - Graphs 16

  17. Cycles The cycle C n , n ≥ 3 , consists of n vertices v 1 , v 2 , …, v n and n edges { v 1 , v 2 }, { v 2 , v 3 },…, { v n-1 , v n }, { v n , v 1 }. CS200 - Graphs 17

  18. Wheels n We obtain the wheel W n when we add an additional vertex to the cycle C n , and connect this new vertex to each of the n vertices in C n , by new edges CS200 - Graphs 18

  19. n -Cube ( n -dimensional hypercube) Hypercube 110 111 100 101 010 011 000 001 CS200 - Graphs 19

  20. The degree of a vertex n The degree of a vertex in an undirected graph q the number of edges incident with it q except that a loop at a vertex contributes twice to the degree of that vertex. CS200 - Graphs 20

  21. Example c d b d is “pendant” a g is “isolated” g e f deg(d) = 1 deg(a) = 2 deg(e) = 3 deg(b) = deg(f) = 4 deg(g) = 0 CS200 - Graphs 21

  22. Question What is the degree of c? A. 4 B. 5 C. 6 c d b a g e f CS200 - Graphs 22

  23. Directed Graphs Indegree: number of incoming edges Outdegree: number of outgoing edges w v CS200 - Graphs 23

  24. Some Graph Theorems n Handshaking : Let G=(V,E) be an undirected graph with m edges. Then ∑ 2 m = deg( v ) v ∈ V n An undirected graph has an even number of vertices of odd degree. n Let G=(V,E) be a directed graph. Then ∑ ∑ deg − ( v ) = deg + ( v ) = E v ∈ V v ∈ V CS200 - Graphs 24

  25. Bipartite Graphs n A simple graph on which the vertex set V can be partitioned into two disjoint sets V1 and V2 such that every edge connects a vertex in V1 to one in V2. n Bipartite? b a a b g c c f f d e d e n Theorem: A simple graph is bipartite iff it is possible to assign one of two different colors to each vertex of the graph so that no two adjacent vertices are assigned the same color. CS200 - Graphs 25

  26. Bipartite Graphs n Assign colors b a a b g c c f f d e d e Theorem: A graph G is bipartite iff it contains no odd cycle CS200 - Graphs 26

  27. Question n Is this graph bipartite? A. Yes e B. No c a f d b CS200 - Graphs 27

  28. Connected Components n An undirected graph is called connected if there is a path between every pair of vertices of the graph. n A connected component of a graph G is a connected subgraph of G that is not a proper subgraph of another connected subgraph of G. G={{a,b,c,d,e,f,g},E} e c a g d b f G 1 ={{a,b,c},E 1 } G 2 ={{d,e,f,g}, E 2 } CS200 - Graphs 28

  29. Question n How many connected components does it have? A. 0 e B. 1 c C. 2 a f d b CS200 - Graphs 29

  30. Connectedness in Directed Graphs n A directed graph is strongly connected if there is a path from a to b and from b to a for all vertices a and b in the graph. n A directed graph is weakly connected if there is a path between every two vertices in the underlying undirected graph. CS200 - Graphs 30

  31. A/B strongly/weakly connected? a d d a e e b c b c Graph A Graph B CS200 - Graphs 31

  32. Graph Data Structures - Adjacency Matrix n Vertices q row and column indices mapped to labels q one vertex mapped to one index n Edges q entries in a square matrix size = (number of vertices)^2 n edge: two (vertex) indices n q values: boolean to indicate presence/absence of edge in n (un)directed graph int to indicate value of weighted edge (0: no edge) n n useful for dense graphs CS200 - Graphs 32

  33. Adjacency Matrix Example For undirected graph, what would adjacency A matrix look like? 0 1 2 3 4 B C 0 0 1 0 1 0 1 0 0 0 0 1 Label Index A 0 2 1 0 0 0 0 B 1 D 3 0 1 0 0 0 C 2 E 4 0 0 1 0 0 D 3 E 4 In a weighted Adjacency Matrix: graph, cells would mapping of vertex array of edges indexed contain weights labels to array indices by vertex number CS200 - Graphs 33

  34. Question Is this an undirected graph? A. Yes B. No 0 1 2 3 4 0 0 1 1 0 0 1 1 0 0 1 1 2 1 0 0 0 1 3 0 1 0 0 0 4 0 1 1 0 0 Adjacency Matrix: CS200 - Graphs 34

  35. Question Is this a simple graph? A. Yes B. No 0 1 2 3 4 0 0 1 1 0 0 1 1 0 0 1 1 2 1 0 0 0 1 3 0 1 0 1 0 4 0 1 1 0 0 Adjacency Matrix: CS200 - Graphs 35

  36. Graph Data Structures - Adjacency List n Vertices q mapped to list of adjacencies A q adjacency: edge B C n Edges: lists of adjacencies q linked-list of out-going edges per vertex D n useful for sparse graphs E CS200 - Graphs 36

  37. Adjacency List: Undirected Graph ArrayList ArrayLists Index Label A B C D 0 A B C A D E 1 B A E 2 C D 3 D A B E 4 E B C mapping of vertex labels to list of edges CS200 - Graphs 37

  38. Adjacency List: Directed Graph ArrayList A ArrayLists Index Label B B D B 0 A B C E 1 B A 2 C This representation B 3 D D is used in Graph C 4 E E recitation and assignment CS200 - Graphs 38

  39. Which Implementation Is Best? n Which implementation best supports common Graph Operations: q Is there an edge between vertex i and vertex j? q Find all vertices adjacent to vertex j n Which best uses space? CS200 - Graphs 39

Recommend


More recommend