Graphs

10
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called edges or arcs. directed graph (digraph) e = (v, w) v: origin of e w: destination of e v w u v w u e e (v, w) = (w, v) undirected graph

description

Graphs. A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called edges or arcs. undirected graph. directed graph (digraph). v. u. v. u. e. e. w. w. e = (v, w). (v, w) = (w, v). - PowerPoint PPT Presentation

Transcript of Graphs

Page 1: Graphs

GraphsA graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called edges or arcs.

directed graph (digraph)

e = (v, w)

v: origin of ew: destination of e

v

w

u v

w

u

e e

(v, w) = (w, v)

undirected graph

Page 2: Graphs

Applications of GraphsJava has a sophisticated Map interface, which has an extensive collection of methods and “views”. Here we present a stripped-down version.

Page 3: Graphs

Some Terminologies• The vertices are adjacent to one another and that the edge

is incident to both vertices. • The degree of a vertex is the number of edges incident to it.• A path is a sequence of vertices such that each adjacent

pair of vertices is connected by an edge.• A cycle is a path with at least one edge whose first and last

vertices are the same.• The length of a path or a cycle is its number of edges it

traverses.• The distance from one vertex to another is the length of the

shortest path from one to the other.

Page 4: Graphs

Some Terminologies

• An undirected graph is connected if there is a path from every vertex to every other vertex in the graph. A graph that is not connected consists of a set of connected components, which are maximal connected subgraphs.

• A directed graph is strongly connected if for every pair of nodes u and v there is a path from u to v and a path from v to u.

Page 5: Graphs

A few common graphs problems

Page 6: Graphs

Representing Graphs

We need to represent the vertices and the edges of a graph so that for any given vertex we can easily identify its neighbors

An adjacency matrix is a V-by-V array of boolean values. Each row and column represents a vertex of the graph. Set the value at row i, column j to true if (i, j) is an edge of the graph. If the graph is undirected, the adjacency matrix is symmetric: row i, column j has the same valueas row j, column i.Space Requirement?

Page 7: Graphs

Representing Graphs

An adjacency list is actually a collection of lists. Each vertex v maintains a list of the edges directed out from v.

Space Requirement?

Page 8: Graphs

Representing Graphs

An adjacency list is actually a collection of lists. Each vertex v maintains a list of the edges directed out from v.

The standard list representations all work — arrays, linked lists, even binary search trees (because you can traverse one in linear time). The total memory used by all the lists is O(V + E).

Page 9: Graphs

Adjacency Matrix vs. Adjacency List

An adjacency list is more space- and time-efficient than an adjacency matrix for a sparse graph, but less efficient for acomplete graph. A complete graph is a graph having every possible edge; that is, for every vertex u and every vertex v, (u, v) is an edge of the graph.

Page 10: Graphs

Depth-First SearchDFS starts at an arbitrary vertex and searches a graph as “deeply” as possible and as early as possible. For example, if your graph is an undirected tree rooted at the starting vertex, DFS performs a postorder tree traversal.