Lecture 8 - Graphs

  1. Graph fundamentals
  2. Graph implementation
  3. Depth-first search
  4. Breadth-first search
  5. Directed graphs

Graphs

Example

Alt text

Edge types

Might want to have a directed edge (can go in one direction, but can't go back)

Alt text

Terminology

Alt text

More terminology

Alt text

More terminology

Alt text

Subgraphs

Alt text

Connectivity

Alt text

Trees and Forests

Alt text

Spanning Trees and Forests

Alt text

Notation

vdeg(v)=2m \sum_{v} deg(v) = 2m

Graph Density

𝐷=2𝑚𝑛(𝑛1)𝐷 = \dfrac{2𝑚}{𝑛(𝑛−1)}

𝐷=m𝑛(𝑛1)𝐷 = \dfrac{m}{𝑛(𝑛−1)}

Graph implementation - vertex and edge ADTs

Graph ADT

function Description
numvertices Returns the number of vertices in the graph
vertices() Returns an iteration of all the vertices of the graph
numEdges() Returns the number of edges in the path
edges() returns an iteration of all the edges of the graph
getEdge(u,v) Returns the edge from vertex uu to verte vv if one exists, otherwise return null.
endVertices(e) Returns an array containing the two endpoint vertices of edge ee. IOf the graph is directed, the first vertex is the origin and the second is the destination
opposite(v, e) For edge ee incident to vertex vv, returns the other vertex of the edge. An error occurs if ee is not incident to vv.
outDegree(v) Returns the number of outgoing endes from vertex vv.
inDegree(v) Returns the number of incvoming edges to vertex vv
outgoingEdges(v) Returns an iteration of all outgoing edges from vertex v
incomingEdges(v) Returns an iterations of all incoming edges to vertex vv
insertEdge(u, v, x) Creates and returns a new Vertex storing element xx.
removeVertex(v) Removes vertex vv and all its incident edges from the graph.
removeEdge(e) Removes edge ee from the graph

Edge list

Alt text

Edge list Structure

Alt text

Adjacency List Structure

Alt text

Adjacency Map Structure

Alt text

Adjacency Matrix structure

Alt text

Performance table

Edge list Adjacency List Adjacency Matrix
Space n+mn+m n+mn+m n2n^{2}
outgoingEdges(v) m deg(vv) nn
incomingEdge(v) m deg(vv) nn
getEdge(v, w) m min(deg(v), deg(w)) 1
insertVertex(o) 1 1 n2n^{2}
insertEdge(v, w, o) 1 1 1
removeVertex(v) m deg(v) n2n^{2}
removeEdge(e) 1 1 1

Graph traversal

Depth-First Seach (DFS)

Algorithm DFS(G, v): Input Graph G and a vertex v of G Output Collection of vertices reachable from v and their discovery edges and back edges Mark vertex v as visited for all e in G.outgoingEdges(v) do if e is not explored then w <- G.opposite(v, e) if w has not been visited then Record edge e as a discovery edge for vertex w DFS(G, w) else Mark e as a back edge for vertex w

Alt text

properties of DFS

Analysis of DFS

Path Finding And Cycle Finding

DFS for entire graph

Algorithm DFS(G): Input Graph G Output Labelling of edges of G as discovery and back edges for all u is an element of G.vertices() do Set u to be unvisited for all e is an element of G.edges() do Set e to be unexplored for all v is an element of G.vertices() do if v has not been visited then DFS(G, v)

BFS Algorithm from a vertex

Algorithm BFS(G, u) Input Graph G and a vertex u of G Output Collection of vertices reachable from u and their discovery and cross edges Q = new empty queue Q.enqueue(u) Mark vertex u as visited while Q.isEmpty() do v = Q.dequeue() for all e in G.incidentEdges(v) do if e is not explored then w <- G.opposite(v, e) if w has not been visited then Record edge e as a discovery edge for vertex w Q.enqueue(w) Mark vertex w as visited else Mark e as a cross edge

Example

Alt text

Alt text

DFS vs. BFS

Back edge (v, w)

BSF Properties

Analysis of BFS

Directed Graphs

Alt text

Digraph Properties

Digraph Application

Scheduling: edge (a,b) means task a must be completed before b can be started

Alt text

Directed Graph Traversal

Reachability

DFS tree rooted at vertex v

Alt text

Strong connectivity

Strong Connectivity Algorithm

Alt text

DAGs and Topological Ordering

Theorem

A digraph has a topological ordering if and only if it is a DAG

Alt text

Algorithim for Topological sorting

Algorithm TopologicalSort(G): H <- G // Temporary copy of G n <- G.numVertices() while H is not empty do Let v be a vertex with no outgoing edges Label v <- n n <- n - 1 Remove v from H

Topological sorting with DFS

Algorithm topologicalDFS(G) Input dag G Output topological ordering of G n <- G.numVertices() for all u in G.vertices() do Set u to be unvisited for all v in G.vertices() do if v has not been visited then topologicalDFS(G, v) Algorithm topologicalDFS(G, v) Input graph G and a start vertex v of G Output labeling of the vertices of G in the connected component of v #Mark vertex v as visited for all e in G.outgoingEdges(v) do w <- G.opposite(v, e) if w has not been visited then { e is a discovery edge } topologicalDFS(G, w) else { e is a forward or cross edge } #Label v with topological number n n <- n - 1

Alt text