Presentation is loading. Please wait.

Presentation is loading. Please wait.

ICOM 4035 – Data Structures Lecture 15 – Graph ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.

Similar presentations


Presentation on theme: "ICOM 4035 – Data Structures Lecture 15 – Graph ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel."— Presentation transcript:

1 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

2 Lecture Organization Part I – Introduction to the Graph ADT Part II – Implementation of the Graph ADT using an adjacency lists M. Rodriguez-MartinezICOM

3 Objectives Introduce the concept of a graph – Collection of nodes interconnected by edges Discuss design and implementation of graph adt Provide motivating examples ICOM 4035M. Rodriguez-Martinez 3

4 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-MartinezICOM

5 Part I Introduction to the Graph ADT M. Rodriguez-MartinezICOM

6 Uses of graphs Graphs – 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-MartinezICOM

7 Introduction to Graphs M. Rodriguez-MartinezICOM Bob Ned Apu San Juan Ponce Hatillo Fajardo Mayaguez Aguadilla Collection of nodes interconnected by edges Money debtor Roads between cities

8 Introduction to Graphs (2) M. Rodriguez-MartinezICOM UPRM UPR AC UPRRP UPR RCM UPRA Classic use for graphs: network modeling

9 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 M. Rodriguez-MartinezICOM Bob Ned Apu Money debtor Directed Graph

10 Undirected Graph 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) M. Rodriguez-MartinezICOM Undirected Graph San Juan Ponce Hatillo Fajardo Mayaguez Aguadilla Roads between cities

11 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-MartinezICOM

12 Disconnected Graph Notice that Aguadilla and Mayaguez are disconnected M. Rodriguez-MartinezICOM San Juan Ponce Hatillo Fajardo Mayaguez Aguadilla Roads between cities

13 Path in Graph Path: Given a graph G = (V, E), a path P in G is a collection of edges P = {e 1, e 2, …, e k} such that e 1 = (n1, n2), e 2 (n2, n3), …, e k = (n i-1, n i ). – A path is made by connecting consecutive edges Alternative notation: P = {n 1, n 2, n 3,...,n i-1, n i } – Left to right order is important. – Leftmost starts the path Paths can have cycles – One or more nodes repeat M. Rodriguez-MartinezICOM

14 Paths in a graph Example Paths: – P1 = {San Juan, Ponce, Mayaguez, Aguadilla, Hatillo, San Juan, Fajardo} – P2 = {Ponce, San Juan, Fajardo} M. Rodriguez-MartinezICOM San Juan Ponce Hatillo Fajardo Mayaguez Aguadilla Roads between cities

15 Part II Implementation of the Graph ADT using an adjacency lists M. Rodriguez-MartinezICOM

16 Representation of Graphs There two main methods to implement Graphs – Adjacency Matrix – Adjacency List M. Rodriguez-MartinezICOM

17 Adjacency Matrix M. Rodriguez-MartinezICOM Bob Ned Apu BobNedApu Bob001 Ned001 Apu000 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

18 Adjacency Lists M. Rodriguez-MartinezICOM Bob Ned Apu Bob Ned Apu 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.

19 Summary Introduced the concept a Graph – Unbalanced ordered tree Discussed application of Graphs Illustrated implementation of directed Graphs using adjacency lists M. Rodriguez-MartinezICOM

20 Questions? – ICOM 4035M. Rodriguez-Martinez 20


Download ppt "ICOM 4035 – Data Structures Lecture 15 – Graph ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel."

Similar presentations


Ads by Google