Data Structures for Graphs Edge list Adjacency lists

  • Slides: 10
Download presentation
Data Structures for Graphs • Edge list • Adjacency lists • Adjacency matrix 1

Data Structures for Graphs • Edge list • Adjacency lists • Adjacency matrix 1

Data Structures for Graphs • A Graph! How can we represent it? • To

Data Structures for Graphs • A Graph! How can we represent it? • To start with, we store the vertices and the edges into two containers, and each edge object has references to the vertices it connects. Additional structures can be used to perform efficiently the methods of the Graph ADT 2

Edge List • The edge list structure simply stores the vertices and the edges

Edge List • The edge list structure simply stores the vertices and the edges into unsorted sequences. • Easy to implement. • Finding the edges incident on a given vertex is inefficient since it requires examining the entire edge sequence 3

Performance of the Edge List Structure Operation Time size, is. Empty, replace. Element, swap

Performance of the Edge List Structure Operation Time size, is. Empty, replace. Element, swap O(1) num. Vertices, num. Edges O(1) vertices O(n) edges, directed. Edges, undirected. Edges O(m) elements, positions O(n+m) end. Vertices, opposite, origin, destination, is. Directed O(1) incident. Edges, in. Incident. Edges, out. Incident. Edges, adjacent Vertices , in. Adjacent. Vertices, out. Adjacent. Vertices, are. Adjacent, degree, in. Degree, out. Degree O(m) insert. Vertex, insert. Edge, insert. Directed. Edge, remove. Edge, make. Undirected, reverse. Direction, set. Direction. From, set. Direction. To O(1) remove. Vertex O(m) 4

Adjacency List (traditional) • adjacency list of a vertex v: sequence of vertices adjacent

Adjacency List (traditional) • adjacency list of a vertex v: sequence of vertices adjacent to v • represent the graph by the adjacency lists of all the vertices Space = (N + deg(v)) = (N + M) 5

Adjacency List (modern) • The adjacency list structure extends the edge list structure by

Adjacency List (modern) • The adjacency list structure extends the edge list structure by adding incidence containers to each vertex. The space requirement is O(n + m). 6

Performance of the Adjacency List Structure size, is. Empty, replace. Element, swap O(1) num.

Performance of the Adjacency List Structure size, is. Empty, replace. Element, swap O(1) num. Vertices, num. Edges O(1) vertices O(n) edges, directed. Edges, undirected. Edges O(m) elements, positions O(n+m) end. Vertices, opposite, origin, destination, is. Directed, degree, in. Degree, out. Degree O(1) incident. Edges(v), in. Incident. Edges(v), out. Incident. Edges(v), adjacent. Vertices(v), in. Adjacent. Vertices(v), out. Adjacent. Vertices(v) O(deg(v)) are. Adjacent(u, v) O(min(deg(u ), deg(v))) insert. Vertex, insert. Edge, insert. Directed. Edge, remove. Edge, make. Undirected, reverse. Direction, O(1) remove. Vertex(v) O(deg(v)) 7

Adjacency Matrix (traditional) • • matrix M with entries for all pairs of vertices

Adjacency Matrix (traditional) • • matrix M with entries for all pairs of vertices M[i, j] = true means that there is an edge (i, j) in the graph. M[i, j] = false means that there is no edge (i, j) in the graph. There is an entry for every possible edge, therefore: Space = (N 2) 8

Adjacency Matrix (modern) • The adjacency matrix structures augments the edge list structure with

Adjacency Matrix (modern) • The adjacency matrix structures augments the edge list structure with a matrix where each row and column corresponds to a vertex. 9

Performance of the Adjacency Matrix Structure 10

Performance of the Adjacency Matrix Structure 10