Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Intro to Graphs CSIT 402 Data Structures II Graphs • Graphs are composed of node › Nodes (vertices) • Can be labeled › Edges (arcs) • Directed (arrowhead) • Undirected • Can be labeled CSIT 402 Graph Introduction edge 2 Graphs: Data Structure Progression Consider previous data structures •Linked List: Nodes with 1 incoming edge and 1 outgoing edge node Value Next node Value Next 94 •Binary Trees/Heaps: Nodes with 1 incoming edge and 2 outgoing edges •General Trees: Nodes with 1 incoming edge and any number of outgoing edges Idea: All of the above are graphs More examples on next seven slides… CSIT 402 Graph Introduction 97 10 96 99 3 Course Prerequisites 461 373 413 321 326 142 415 410 322 143 370 341 417 378 Nodes = courses Directed edge = prerequisite CSIT 402 Graph Introduction 421 401 4 Representing a Maze Directed or Undirected? S S B E E Nodes = locations Edge = paths between locations CSIT 402 Graph Introduction 5 Representing Electrical Circuits Battery Switch Directed or Undirected? Nodes = battery, switch, resistor, etc. Edges = connections CSIT 402 Graph Introduction Resistor 6 Program statements Directed or Undirected? x1=q+y*z x2=y*z-q Naive: x1 x2 + * q y y*z calculated twice common subexpression eliminated: * Nodes = symbols/operators Edges = relationships CSIT 402 Graph Introduction z x1 x2 + - q * y q q z 7 Precedence S1 S2 S3 S4 S5 S6 a=0; b=1; c=a+1 d=b+a; e=d+1; e=c+d; Directed or Undirected? 6 5 Which statements must execute before S6? S1, S2, S3, S4 3 4 Values Nodes = statements Edges = precedence requirements 1 CSIT 402 Graph Introduction 2 8 Information Transmission in a Computer Network Directed or Undirected? 56 Tokyo Seattle Seoul 128 16 30 Sydney New York 181 140 L.A. Nodes = computers Edges = transmission rates Values CSIT 402 Graph Introduction 9 Traffic Flow on Highways Directed or Undirected? UW Nodes = cities Edges = # vehicles on connecting highway CSIT 402 Graph Introduction 10 Formal Definition • A graph G is a tuple (V, E) where › › › V is a set of vertices or nodes E is a set of edges • An Edge e is a pair (v1,v2), where vi ε V Example: • V = {A, B, C, D, E, F} • E = {(A,B), (A,D), (B,C), (C,D), (C,E), (D,E)} B C A F D CSIT 402 Graph Introduction E 11 Directed vs Undirected Graphs • If the order of edge pairs (vi, vj) is important (e.g prerequisites), the graph is directed (also called a digraph): (vi, vj) (vj, vi) , i j vj vi • If the order of edge pairs (vi, vj) is unimportant (e.g. maze), the graph is called an undirected graph (never called an undigraph): In this case, (vi, vj) = (vj, vi) , i j vi vj CSIT 402 Graph Introduction 12 Undirected Graph Terminology • Two vertices u and v are adjacent in an undirected graph G if {u,v} is an edge in G › edge e = {u,v} is incident (in contact) with vertex u and vertex v • The degree of a vertex in an undirected graph is the number of edges incident with it › a self-loop counts twice (both ends count) B is adjacent to C and C is adjacent to B › denoted with deg(v) B (A,B) is incident to A and to B C Self-loop A F Deg(D) = 3 D CSIT 402 Graph Introduction E Deg(F) = 0 13 Directed Graph Terminology • Vertex u is adjacent to vertex v in a directed graph G if (u,v) is an edge in G › vertex u is the initial vertex of (u,v) • Vertex v is adjacent from vertex u › vertex v is the terminal (or end) vertex of (u,v) • Degree › in-degree is the number of edges with the vertex as the terminal vertex › out-degree is the number of edges with the vertex as the initial vertex CSIT 402 Graph Introduction 14 Directed Graph Terminology B adjacent to C and C adjacent from B B C A F D E In-degree = 2 Out-degree = 1 CSIT 402 Graph Introduction In-degree = 0 Out-degree = 0 15 Handshaking Theorem • Let G=(V,E) be an undirected graph with |E|=e edges. Then 2e deg(v) Add up the degrees of all vertices. vV • Every edge contributes +1 to the degree of each of the two vertices it is incident with › number of edges is exactly half the sum of deg(v) › the sum of the deg(v) values must be even CSIT 402 Graph Introduction 16 Graph Representations • Space and time are analyzed in terms of: • Number of vertices = |V| and • Number of edges = |E| • There are at least two ways of representing graphs: • The adjacency matrix representation • The adjacency list representation CSIT 402 Graph Introduction 17 Adjacency Matrix B C A F D E 1 if (v, w) is in E M(v, w) = 0 otherwise A B C D E F A 0 1 0 1 0 0 B 1 0 1 0 0 0 C 0 1 0 1 1 0 D 1 0 1 0 1 0 E 0 0 1 1 0 0 F 0 0 0 0 0 0 Space = |V|2 CSIT 402 Graph Introduction 18 Adjacency Matrix for a Digraph B C A F D E 1 if (v, w) is in E M(v, w) = 0 otherwise A B C D E F A 0 1 0 1 0 0 B 0 0 1 0 0 0 C 0 0 0 1 1 0 D 0 0 0 0 1 0 E 0 0 0 0 0 0 F 0 0 0 0 0 0 Space = |V|2 CSIT 402 Graph Introduction 19 Adjacency List For each v in V, L(v) = list of w such that (v, w) is in E a b B C A F D E list of neighbors A B D B A C C B D E D A C E E C D F CSIT 402 Graph Introduction Space = |V| + 2 |E| 20 Adjacency List for a Digraph For each v in V, L(v) = list of w such that (v, w) is in E a b B C A F D E A B B C C D D E D E E F CSIT 402 Graph Introduction Space = |V| + |E| 21 Paths and Cycles • Given a directed graph G = (V,E), a path is a sequence of vertices v1,v2, …,vk such that: › (vi,vi+1) in E for 1 < i < k • path length = number of edges in the path • path cost = sum of costs of each edge (if edges have costs) • A graph contains a cycle if, there exists a path v1,v2, …,vk, k > 1 such that vk = v1 • G is acyclic if it has no cycles. CSIT 402 Directed 22 Graphs