Intro to Graphs CSIT 402 Data Structures II
CSIT 402 Graph Introduction2 Graphs Graphs are composed of ›Nodes (vertices) Can be labeled ›Edges (arcs) Directed (arrowhead) Undirected Can be labeled node edge
CSIT 402 Graph Introduction3 Graphs: Data Structure Progression Consider previous data structures Linked List: Nodes with 1 incoming edge and 1 outgoing edge 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… Value Next node Value Next node
CSIT 402 Graph Introduction4 Course Prerequisites Nodes = courses Directed edge = prerequisite
CSIT 402 Graph Introduction5 Representing a Maze S Nodes = locations Edge = paths between locations S E B E Directed or Undirected?
CSIT 402 Graph Introduction6 Representing Electrical Circuits Nodes = battery, switch, resistor, etc. Edges = connections Battery Switch Resistor Directed or Undirected?
CSIT 402 Graph Introduction7 Program statements x1=q+y*z x2=y*z-q Naive: common subexpression eliminated: yz * - q + q * x1x2 yz - q + q* x1x2 Nodes = symbols/operators Edges = relationships y*z calculated twice Directed or Undirected?
CSIT 402 Graph Introduction8 Precedence S 1 a=0; S 2 b=1; S 3 c=a+1 S 4 d=b+a; S 5 e=d+1; S 6 e=c+d; Which statements must execute before S 6 ? S 1, S 2, S 3, S 4 Nodes = statements Edges = precedence requirements Directed or Undirected? Values
CSIT 402 Graph Introduction9 Information Transmission in a Computer Network Seattle L.A. Tokyo Seoul Nodes = computers Edges = transmission rates Directed or Undirected? Values
CSIT 402 Graph Introduction10 Traffic Flow on Highways Nodes = cities Edges = # vehicles on connecting highway UW Directed or Undirected?
CSIT 402 Graph Introduction11 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 (v 1,v 2 ), where v i ε V ›Example: V = {A, B, C, D, E, F} E = {(A,B), (A,D), (B,C), (C,D), (C,E), (D,E)} A B C E D F
If the order of edge pairs (v i, v j ) is important (e.g prerequisites), the graph is directed (also called a digraph): (v i, v j ) (v j, v i ), i j If the order of edge pairs (v i, v j ) is unimportant (e.g. maze), the graph is called an undirected graph (never called an undigraph): In this case, (v i, v j ) = (v j, v i ), i j CSIT 402 Graph Introduction12 Directed vs Undirected Graphs vivi vjvj vivi vjvj
CSIT 402 Graph Introduction13 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) ›denoted with deg(v) A B C E D F Deg(D) = 3 Deg(F) = 0 (A,B) is incident to A and to B Self-loop B is adjacent to C and C is adjacent to B
CSIT 402 Graph Introduction14 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 Introduction15 Directed Graph Terminology A B C E D F In-degree = 2 Out-degree = 1 In-degree = 0 Out-degree = 0 B adjacent to C and C adjacent from B
CSIT 402 Graph Introduction16 Handshaking Theorem Let G=(V,E) be an undirected graph with |E|=e edges. Then 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 Add up the degrees of all vertices.
CSIT 402 Graph Introduction17 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 Graph Representations
CSIT 402 Graph Introduction18 A B C D E F M(v, w) = 1 if (v, w) is in E 0 otherwise ABCDEFABCDEF Space = |V| 2 A B C E D F Adjacency Matrix
CSIT 402 Graph Introduction19 A B C D E F ABCDEFABCDEF Space = |V| 2 M(v, w) = 1 if (v, w) is in E 0 otherwise A B C E D F Adjacency Matrix for a Digraph
CSIT 402 Graph Introduction20 B D BD C A C E D E A C ABCDEFABCDEF A B C E D F Space = |V| + 2 |E| For each v in V, L(v) = list of w such that (v, w) is in E a b Adjacency List list of neighbors
CSIT 402 Graph Introduction21 B D E D C a b ABCDEFABCDEF E A B C E D F For each v in V, L(v) = list of w such that (v, w) is in E Space = |V| + |E| Adjacency List for a Digraph
CSIT 402 Directed Graphs 22 Paths and Cycles Given a directed graph G = (V,E), a path is a sequence of vertices v 1,v 2, …,v k such that: ›(v i,v i+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 v 1,v 2, …,v k, k > 1 such that v k = v 1 G is acyclic if it has no cycles.