ICOM 4035 – Data Structures Lecture 15 – Graph ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel Rodriguez – All rights reserved
Lecture Organization Part I – Introduction to the Graph ADT Part II – Implementation of the Graph ADT using an adjacency lists M. Rodriguez-Martinez ICOM 4035
Objectives Introduce the concept of a graph Collection of nodes interconnected by edges Discuss design and implementation of graph adt Provide motivating examples M. Rodriguez-Martinez ICOM 4035
Companion videos Lecture15 videos Contains the coding process associated with this lecture Shows how to build the interfaces, concrete classes, and factory classes mentioned here M. Rodriguez-Martinez ICOM 4035
Part I Introduction to the Graph ADT M. Rodriguez-Martinez ICOM 4035
Uses of graphs Graphs Graphs are used to represent: Collection of nodes connected by edges G = (V,E) V = collection of nodes E = collection of edges Graphs are used to represent: Roads/airline routes between cities Relationships between persons Facebook friendship Connections between computers on a network Job scheduling An many more … M. Rodriguez-Martinez ICOM 4035
Introduction to Graphs San Juan Bob Hatillo Fajardo Apu Ned Aguadilla Ponce Money debtor Mayaguez Roads between cities Collection of nodes interconnected by edges M. Rodriguez-Martinez ICOM 4035
Introduction to Graphs (2) UPRRP UPRM UPR RCM UPRA UPR AC Classic use for graphs: network modeling M. Rodriguez-Martinez ICOM 4035
Directed Graph Directed Graph A Graph G=(V,E) is directed graph whenever the edges have an associated direction i.e., edge (a,b) is different from (b,a) or (b,a) might not be present Bob Directed Graph Apu Ned Money debtor M. Rodriguez-Martinez ICOM 4035
Undirected Graph Undirected Graph San Juan A Graph G=(V,E) is undirected graph whenever the edges have no associated direction i.e., edge (a,b) is the same as (b,a) Hatillo Fajardo Aguadilla Ponce Mayaguez Roads between cities Undirected Graph M. Rodriguez-Martinez ICOM 4035
Graph vs Tree A Tree is a special case of a undirected graph In general, graphs: Have no notion of root node Have no notion of leaf nodes Can have cycles Can be disconnected M. Rodriguez-Martinez ICOM 4035
Disconnected Graph Notice that Aguadilla and Mayaguez are disconnected San Juan Hatillo Fajardo Notice that Aguadilla and Mayaguez are disconnected Aguadilla Ponce Mayaguez Roads between cities M. Rodriguez-Martinez ICOM 4035
Path in Graph Path: Given a graph G = (V, E), a path P in G is a collection of edges P = {e1, e2, …, ek} such that e1 = (n1, n2), e2 (n2, n3), …, ek = (ni-1, ni). A path is made by connecting consecutive edges Alternative notation: P = {n1, n2, n3, ...,ni-1, ni} Left to right order is important. Leftmost starts the path Paths can have cycles One or more nodes repeat M. Rodriguez-Martinez ICOM 4035
Paths in a graph Example Paths: San Juan Example Paths: P1 = {San Juan, Ponce, Mayaguez, Aguadilla, Hatillo, San Juan, Fajardo} P2 = {Ponce, San Juan, Fajardo} Hatillo Fajardo Aguadilla Ponce Mayaguez Roads between cities M. Rodriguez-Martinez ICOM 4035
Part II Implementation of the Graph ADT using an adjacency lists M. Rodriguez-Martinez ICOM 4035
Representation of Graphs There two main methods to implement Graphs Adjacency Matrix Adjacency List M. Rodriguez-Martinez ICOM 4035
Adjacency Matrix Bob Ned Apu 1 Bob Apu Ned You use a boolean matrix 1 Bob Apu Ned You use a boolean matrix 0 means there is no edge between nodes a,b 1 means there is an edge between nodes a,b Wastes too much space if matrix is sparse M. Rodriguez-Martinez ICOM 4035
Adjacency Lists Bob Apu Ned You use an array of linked lists Each node has a list with the nodes that are adjacent to it. Saves space, and is easier to implement. M. Rodriguez-Martinez ICOM 4035
Summary Introduced the concept a Graph Discussed application of Graphs Unbalanced ordered tree Discussed application of Graphs Illustrated implementation of directed Graphs using adjacency lists M. Rodriguez-Martinez ICOM 4035
Questions? Email: manuel.rodriguez7@upr.edu M. Rodriguez-Martinez ICOM 4035