graphs. motivating problem konigsberg bridge problem (1736): konigsberg bridge problem (1736): c a b...

67
Graphs Graphs

Upload: nora-moore

Post on 27-Dec-2015

227 views

Category:

Documents


1 download

TRANSCRIPT

GraphsGraphs

Motivating ProblemMotivating Problem

Konigsberg bridge problem (1736):Konigsberg bridge problem (1736):

C

A

B

D

a b

c d

e

f

g

Starting in one land area, is it possible to walk across all bridges once and return to the initial land area?

The answer is no, but how do we prove that?

Konigsberg = Graph Konigsberg = Graph ProblemProblem

The Konigsberg is an instance of a The Konigsberg is an instance of a graph problemgraph problem

Definition of a graph:Definition of a graph: Graph G: Consists of two sets, V and EGraph G: Consists of two sets, V and E V: A finite, non-empty set of verticesV: A finite, non-empty set of vertices E: A set of pairs of vertices, where the E: A set of pairs of vertices, where the

pairs are called edges.pairs are called edges.

Example GraphsExample Graphs

0

1 2

3

V: 0, 1, 2, 3

E: (0,1), (0,2), (0,3) (1,2), (1,3), (2,3)

0

1 2

3

V: 0, 1, 2, 3

E: Empty

0

1 2

3

V: 0, 1, 2, 3

E: (0,1), (0,2), (1,3)

Trees are a subset of graphs

Graph DefinitionsGraph Definitions

Undirected graph: Pair of vertices Undirected graph: Pair of vertices representing any edge is unorderedrepresenting any edge is unordered (u,v) is the same edge as (v,u)(u,v) is the same edge as (v,u)

Directed graph: Each edge is Directed graph: Each edge is represented by a directed pair (u,v)represented by a directed pair (u,v) Drawn with an arrow from u to v Drawn with an arrow from u to v

indicating the direction of the edgeindicating the direction of the edge (u,v) is not the same edge as (v,u)(u,v) is not the same edge as (v,u)

Directed vs. UndirectedDirected vs. Undirected

0

1 2

3

0

1 2

3

0

1 2

3

Graph A: Undirected(1,0) is a valid edgeSame as (0,1) edge

Graph B: DirectedNot equivalent to A(1,0) not valid edge

Graph C: DirectedEquivalent to A(1,0) is a valid edgeA (0,1) edge also exists

Graph RestrictionsGraph Restrictions

For now lets assume vertices and For now lets assume vertices and edges are setsedges are sets No self edges (vertice back to itself)No self edges (vertice back to itself) No repeated edges (multigraph)No repeated edges (multigraph)

0

1 2

3

Repeated

Self

Motivating Problem: Graph Motivating Problem: Graph RestrictionsRestrictions

Konigsberg bridge problem (1736):Konigsberg bridge problem (1736):

0

1 3

2

This is the appropriate graph representation.

We’re not going to solve it for now because of our assumption of no repeated edges.

Graph DefinitionsGraph Definitions

Maximum possible number of Maximum possible number of distinct unordered pairs (u,v) distinct unordered pairs (u,v) (undirected graph) in a graph with n (undirected graph) in a graph with n vertices is n*(n-1) / 2.vertices is n*(n-1) / 2.

A graph with this many edges is A graph with this many edges is called a called a complete graphcomplete graph..0

1 2

3

Complete:6 edges = (4 * 3) / 2

0

1 2

3Not Complete:(4*3)/2 != 4 edges

Graph DefinitionsGraph Definitions

Directed Graph:Directed Graph: Maximum of (n * (n-1)) edges. [Twice Maximum of (n * (n-1)) edges. [Twice

that for undirected because 2 directed that for undirected because 2 directed are equivalent to one undirected]are equivalent to one undirected]

Proof of (n * (n – 1)) bounds:Proof of (n * (n – 1)) bounds:n nodes, can point to every other n nodes, can point to every other node except for themselves node except for themselves

n-1 edges connecting to each of n-1 edges connecting to each of the n nodesthe n nodes

Graph DefinitionsGraph Definitions

If (u, v) is an edge in E(G),If (u, v) is an edge in E(G), Vertices u and v are called Vertices u and v are called adjacentadjacent The edge (u,v) is called The edge (u,v) is called incidentincident on on

vertices u and v.vertices u and v. Examples:Examples:

0

1 2

3

Vertex 0 is adjacent to 1 and 2Vertex 1 is adjacent to 0, 2, and 3Vertex 2 is adjacent to 0 and 1Vertex 3 is adjacent to 1

Edges incident on vertex 2:(0,2), (1,2)Edges incident on vertex 3:(1,3)

Graph DefinitionsGraph Definitions

A subgraph of graph G called G’ is a A subgraph of graph G called G’ is a graph such that V(G’) is a subset graph such that V(G’) is a subset V(G) and E(G’) is a subset of E(G)V(G) and E(G’) is a subset of E(G)

0

1 2

3

Subgraphs

0

1 2

1

3

0

1 2

Graph DefinitionsGraph Definitions A A pathpath from vertex u to vertex v in a from vertex u to vertex v in a

graph G is a sequence of vertices u, i1, graph G is a sequence of vertices u, i1, i2, …, ik, v, such that (u,i1), (i1,i2)…(ik,v) i2, …, ik, v, such that (u,i1), (i1,i2)…(ik,v) are edges in G.are edges in G.

If in a directed graph, the edges have to If in a directed graph, the edges have to be in the right direction.be in the right direction.

The The length of a pathlength of a path is the number of is the number of edges on the path.edges on the path.

A A simple pathsimple path is a path in which all is a path in which all vertices except possibly the first and last vertices except possibly the first and last are distinct.are distinct.

Graph DefinitionsGraph Definitions

0

1 2

3

Paths from 1 to 3:(1,3) 1,3 [Simple]Length = 1(1,2), (2,3) 1,2,3 [Simple]Length = 2(1,0),(0,2),(2,1),(1,3) 1,0,2,1,3Length = 4(1,2),(2,0),(0,1),(1,3) 1,2,0,1,3Length = 4(1,0),(0,2),(2,3) 1,0,2,3 [Simple]Length = 3

Many more that repeat internally, Not simple,Length > 4

Graph DefinitionsGraph Definitions

A A cyclecycle is a is a simplesimple path where the path where the first and last vertices are the same.first and last vertices are the same.

0

1 2

3

Cycles to 1:1,0,2,3,11,0,2,11,3,2,11,2,11,0,11,3,1

Graph DefinitionsGraph Definitions

Two vertices u and v are Two vertices u and v are connected connected if there is a path in G from u to v.if there is a path in G from u to v.

An undirected graph is said to be An undirected graph is said to be connected connected (at the graph level) if (at the graph level) if and only if for every pair of distinct and only if for every pair of distinct vertices u and v in V(G) there is a vertices u and v in V(G) there is a path from u to v in G.path from u to v in G.

A connected component of a graph is A connected component of a graph is a maximal connected subgrapha maximal connected subgraph

Graph DefinitionsGraph Definitions

0

1 2

3

4

5 6

7

Graph G4: V(G4): 0,1,2,3,4,5,6,7 E(G4): (0,1), (0,2), (1,3), (2,3), (4,5), (5,6), (6,7)

There are two connected components of G4: H1 (0-3) and H2 (4-7)Verify that H1 and H2 components are connected: Path between all pairs of verticesDirected graphs – different because paths are directed, harder to get connected components

Graph DefinitionsGraph Definitions

A A treetree is a connected, acyclic graph is a connected, acyclic graph For any node there is path to any other For any node there is path to any other

node (usually back through a “parent node (usually back through a “parent node”)node”)

Acyclic property forces a unique path Acyclic property forces a unique path between nodesbetween nodes 0

1 4

32

Graph DefinitionsGraph Definitions

Need a corollary to “connected” for Need a corollary to “connected” for directed graphsdirected graphs

A directed graph G is A directed graph G is strongly strongly connectedconnected if for every pair of if for every pair of distinct vertices in V(G), u and v, distinct vertices in V(G), u and v, there is a directed path there is a directed path from u to v from u to v and from v to u.and from v to u.

A A strongly connected componentstrongly connected component is a maximal subgraph of a directed is a maximal subgraph of a directed graph that is strongly connectedgraph that is strongly connected

Graph DefinitionsGraph Definitions0

1

Graph G5: V(G5): 0,1,2 E(G5): (0,1), (1,0), (1,2)

G5 is not strongly connected (No path from 2 to 1)

There are two strongly connected components of G4: H1 (0-1) and H2 (2)Verify that H1 and H2 components are strongly connected: Directed path between all pairs of vertices

2

Graph DefinitionsGraph Definitions

The The degreedegree of a vertex v is the of a vertex v is the number of edges incident to that number of edges incident to that vertex.vertex.

For a directed graph,For a directed graph, The The in-degreein-degree of a vertex v is the of a vertex v is the

number of edges for which the arrow number of edges for which the arrow points at v. points at v.

The The out-degreeout-degree is defined as the is defined as the number of edges for which the arrow number of edges for which the arrow points away from v.points away from v.

Graph DefinitionsGraph Definitions

0

1 2

3

Degree of Vertices: 0, 3 => Degree 2 1, 2 => Degree 3

0

1 2

0 Out-Degree : 1 In-Degree: 01 Out-Degree : 1 In-Degree: 02 Out-Degree : 0 In-Degree: 2

Graph DefinitionsGraph Definitions

For undirected graph whose vertices v_i For undirected graph whose vertices v_i have degree d_i, the number of edges, |E|, have degree d_i, the number of edges, |E|, is (the sum from 0 to n-1 of degree_i ) / 2is (the sum from 0 to n-1 of degree_i ) / 2 Essentially just counting the edges.Essentially just counting the edges.

Divide by 2 because double counting (if node1, node2 Divide by 2 because double counting (if node1, node2 share an edge, that edge is in both of their degrees)share an edge, that edge is in both of their degrees)

Useful for computing max number of edges if Useful for computing max number of edges if you only know number of vertices and their you only know number of vertices and their degree (ie they are all binary => |E| = 2 * |V| / degree (ie they are all binary => |E| = 2 * |V| / 2 = |V|)2 = |V|)

Given our graph assumptions (no repeated edges, no Given our graph assumptions (no repeated edges, no self edges) degree has to be <= |V|-1self edges) degree has to be <= |V|-1

Graph DefinitionsGraph Definitions

0

1 2

3

Degree of Vertices: 0, 3 => Degree 2 1, 2 => Degree 3|E| = Sum of Degrees / 2 = (2+2+3+3)/2 = 10/2 = 5 {Correct!}

0

1 2

|E| max = Sum of Degrees / 2= (2 + 2 + 2) / 2 = 6/2 = 3{Correct!}

|E|Max = ?,All Binary

Graph RepresentationsGraph Representations

What core functionality do we need What core functionality do we need in representation?in representation? Set of verticesSet of vertices Set of edgesSet of edges

Two major representations:Two major representations: Adjacency matrix [Array based]Adjacency matrix [Array based] Adjacency list [Linked List based]Adjacency list [Linked List based]

Adjacency MatrixAdjacency Matrix

G = (V,E) graph with |V| = n, n >= 1G = (V,E) graph with |V| = n, n >= 1 Adjacency matrix:Adjacency matrix:

2 dimensional n x n array called A with property 2 dimensional n x n array called A with property that A[i][j] = 1 if the edge (i,j) is in E, 0 otherwise.that A[i][j] = 1 if the edge (i,j) is in E, 0 otherwise.

0

1 2

3

V0V0 V1V1 V2V2 V3V3

V0V0 00 11 11 00

V1V1 11 00 00 11

V2V2 11 00 00 11

V3V3 00 11 11 00

Adjacency MatrixAdjacency Matrix

Directed Graphs: Rows are the out Directed Graphs: Rows are the out indicators (data in a row indicates that indicators (data in a row indicates that there is an outgoing link)there is an outgoing link)

0

1 2

3

V0V0 V1V1 V2V2 V3V3

V0V0 00 00 11 00

V1V1 11 00 00 11

V2V2 00 00 00 00

V3V3 00 00 11 00

Adjacency MatrixAdjacency Matrix Note how the adjacency matrix for an Note how the adjacency matrix for an

undirected graph is symmetric. Can save undirected graph is symmetric. Can save approximately half the space by storing approximately half the space by storing only upper triangle or lower triangleonly upper triangle or lower triangle

0

1 2

3

V0V0 V1V1 V2V2 V3V3

V0V0 00 11 11 00

V1V1 11 00 00 11

V2V2 11 00 00 11

V3V3 00 11 11 00

Adjacency MatrixAdjacency Matrix

0

1 2

3

V0V0 V1V1 V2V2 V3V3

V0V0 00 11 11 00

V1V1 11 00 00 11

V2V2 11 00 00 11

V3V3 00 11 11 00

Given a complete adjacency matrix, can easily:Determine if there is an edge between any two vertices (look in appropriate column)

[Undirected Graph] Compute degree of a node (sum over row)

Adjacency MatrixAdjacency MatrixGiven a complete adjacency matrix, can easily: [Directed Graph] Compute out degree of a node (sum over row)

Compute in degree of a node (sum over column)

0

1 2

3

V0V0 V1V1 V2V2 V3V3

V0V0 00 00 11 00

V1V1 11 00 00 11

V2V2 00 00 00 00

V3V3 00 00 11 00

Adjacency MatrixAdjacency Matrix

What if we want to compute a non-What if we want to compute a non-trivial answer?trivial answer? How many total edges are there in the How many total edges are there in the

graph?graph? Is the graph connected?Is the graph connected? Total edges: Requires O(n^2) operations Total edges: Requires O(n^2) operations

(n^2 entries – n [diagonals always 0])(n^2 entries – n [diagonals always 0])0

1 2

3

V0V0 V1V1 V2V2 V3V3

V0V0 00 11 11 00

V1V1 11 00 00 11

V2V2 11 00 00 11

V3V3 00 11 11 00

Adjacency MatrixAdjacency Matrix

What if we have a What if we have a sparse graph?sparse graph? Sparse = Very few Sparse = Very few

connections out of all connections out of all possiblepossible

0

1 2

3

4

5 6

7

VV00

VV11

VV22

VV33

VV44

VV55

VV66

VV77

V0V0 00 11 11 00 00 00 00 00V1V1 11 00 00 11 00 00 00 00V2V2 11 00 00 11 00 00 00 00V3V3 00 11 11 00 00 00 00 00V4V4 00 00 00 00 00 11 00 00V5V5 00 00 00 00 11 00 11 00V6V6 00 00 00 00 00 11 00 11V7V7 00 00 00 00 00 00 11 00

Adjacency MatrixAdjacency Matrix

Would really like to do O(|E|) operations Would really like to do O(|E|) operations when counting edgeswhen counting edges

O(n^2) is a given when using adjacency O(n^2) is a given when using adjacency matrixmatrix

For dense graphs, |E| is close to n^2For dense graphs, |E| is close to n^2 Not for sparse graphs (|E| << n^2)Not for sparse graphs (|E| << n^2)

Solution: Use linked lists and store only those Solution: Use linked lists and store only those edges that are really represented in the edges that are really represented in the graph (no 0’s for things that aren’t present).graph (no 0’s for things that aren’t present). Slightly more complicated to implement but saves Slightly more complicated to implement but saves

a lot of timea lot of time

Adjacency ListAdjacency List

N rows of adjacency matrix N rows of adjacency matrix (vertices) are represented as n (vertices) are represented as n linked lists.linked lists.

Nodes in list i are those nodes Nodes in list i are those nodes adjacent to the corresponding vertex adjacent to the corresponding vertex v_i.v_i. 0

1 2

3

0

1

2

3

1 2

3 0

0 3

1 2

Order in linked list doesn’t matter

Array of Head Node Pointers

Adjacency ListAdjacency List

Undirected Graph:Undirected Graph: n vertices, e n vertices, e edges – Requires n head nodes, 2 * e edges – Requires n head nodes, 2 * e list nodes list nodes

For any vertex, computing degree For any vertex, computing degree (number of incident edges) is (number of incident edges) is counting size of corresponding list.counting size of corresponding list.

Number of edges for whole graph is Number of edges for whole graph is computed in O(n + e) << O(n^2)computed in O(n + e) << O(n^2)

Adjacency List for Directed Adjacency List for Directed GraphGraph

N rows of adjacency matrix N rows of adjacency matrix (vertices) are represented as n (vertices) are represented as n linked lists.linked lists.

Nodes in list i are those nodes that Nodes in list i are those nodes that one can reach from leaving the one can reach from leaving the corresponding vertexcorresponding vertex0

1

2

3

2

3 0

2

Order in linked list doesn’t matter

Array of Head Node Pointers0

1 2

3

Adjacency ListAdjacency List

For a directed graph, nodes in a list For a directed graph, nodes in a list are those that you can reach leaving are those that you can reach leaving from the corresponding vertexfrom the corresponding vertex Computing out degree for any vertex – Computing out degree for any vertex –

Count number of nodes in corresponding Count number of nodes in corresponding list list

Computing number of total edges in Computing number of total edges in graph: Adding all outdegrees => O(n + graph: Adding all outdegrees => O(n + e) [visit all head nodes and all items in e) [visit all head nodes and all items in lists] << O(n^2)lists] << O(n^2)

Adjacency ListAdjacency List

For directed graphs, this approach isn’t For directed graphs, this approach isn’t very useful for determining in-degreevery useful for determining in-degree This was trivial in an adjacency matrix (sum This was trivial in an adjacency matrix (sum

down a column)down a column) You can build the inverse adjacency list at You can build the inverse adjacency list at

the same time building adjacency list.the same time building adjacency list.

0

1

2

3

1

0 3

1

Array of Head Node Pointers0

1 2

3

Primitive Graph Primitive Graph OperationsOperations

In essence, can just think of a graph as a container, In essence, can just think of a graph as a container, holding edges and vertices (with a lot of special holding edges and vertices (with a lot of special properties):properties):

class Graphclass Graph{{

public:public:Graph(); // create an empty graphGraph(); // create an empty graphvoid InsertVertex(Vertex v); // Insert v into graph with no void InsertVertex(Vertex v); // Insert v into graph with no

incident edgesincident edgesvoid InsertEdge(Vertex u, Vertex v); // Insert edge (u,v) into void InsertEdge(Vertex u, Vertex v); // Insert edge (u,v) into

graphgraphvoid DeleteVertex(Vertex v); // Delete v void DeleteVertex(Vertex v); // Delete v and all edges and all edges

incident to itincident to itvoid DeleteEdge(Vertex u, Vertex v); Delete edge (u,v) from void DeleteEdge(Vertex u, Vertex v); Delete edge (u,v) from

graphgraphbool IsEmpty(); // if graph has no vertices return truebool IsEmpty(); // if graph has no vertices return trueList<Vertex> Adjacent(Vertex v); // return list of all vertices List<Vertex> Adjacent(Vertex v); // return list of all vertices

adjacent to vertex vadjacent to vertex v} }

Adjacency Matrix/List Adjacency Matrix/List ConstructionConstruction

Note we will be updating these data Note we will be updating these data structures as we call our graph class structures as we call our graph class methods:methods: InsertVertex(Vertex v)InsertVertex(Vertex v) InsertEdge(Vertex u, Vertex v)InsertEdge(Vertex u, Vertex v) DeleteVertex(Vertex v)DeleteVertex(Vertex v) DeleteEdge(Vertex u, Vertex v)DeleteEdge(Vertex u, Vertex v)

Linked list approach likely more useful Linked list approach likely more useful over arrays if don’t know beforehand over arrays if don’t know beforehand what adding to graph (how many edges, what adding to graph (how many edges, etc).etc).

Weighted EdgesWeighted Edges

Often see weighted edges on graphsOften see weighted edges on graphs Common uses:Common uses:

Distance between verticesDistance between vertices Cost of moving from one vertex to Cost of moving from one vertex to

another vertexanother vertex Think of vertices as cities – could be real Think of vertices as cities – could be real

distances or flight costs and want to distances or flight costs and want to know shortest/cheapest pathknow shortest/cheapest path

How do we incorporate these weights How do we incorporate these weights into our graph representations?into our graph representations?

Weighted EdgesWeighted Edges

01

2

3

Mehran Ilam

Sarableh

Ivan

60

40

55

50

Could store weights in adjacency matrix(just need a non-zero entry)

0 1 2 30 0 60 40 551 60 0 0 02 40 0 0 503 55 0 50 0

If using lists, add another field to node

Elementary Graph Elementary Graph OperationsOperations

First Operation: Traversal of graphsFirst Operation: Traversal of graphs Already saw how this worked in binary Already saw how this worked in binary

trees (inorder, preorder, postorder, depth-trees (inorder, preorder, postorder, depth-order)order)

Similar idea for general graphsSimilar idea for general graphs

Given a graph G = (V,E) and a vertex v Given a graph G = (V,E) and a vertex v in V(G), visit all vertices in G that are in V(G), visit all vertices in G that are reachable from v reachable from v This is the subset of the graph that is This is the subset of the graph that is

connected to v.connected to v.

Graph TraversalGraph Traversal

Depth-First SearchDepth-First Search Similar to descending down binary treeSimilar to descending down binary tree Basic algorithm:Basic algorithm:

Begin by starting at vertex v.Begin by starting at vertex v. Select an edge from v, say it’s the edge (v,w).Select an edge from v, say it’s the edge (v,w). Move to vertex w and recurse.Move to vertex w and recurse. When a node has been visited that has all of When a node has been visited that has all of

its adjacent vertices visited, back up to the its adjacent vertices visited, back up to the last node with an unvisited adjacent vertex last node with an unvisited adjacent vertex and recurse.and recurse.

DFS TraversalDFS Traversal

void Graph::DFS() // drivervoid Graph::DFS() // driver

{{

visited = new bool[n];visited = new bool[n];

for (int i = 0; i < n; i++) visited[i] = false;for (int i = 0; i < n; i++) visited[i] = false;

DFS(0);DFS(0);

delete [] visited;delete [] visited;

}}

void Graph::DFS(const int v) // workhorsevoid Graph::DFS(const int v) // workhorse

{{

visited[v] = true;visited[v] = true;

for (each vertex w adjacent to v) //use adjacency matrix or listsfor (each vertex w adjacent to v) //use adjacency matrix or lists

if (!visited[w]) DFS(w);if (!visited[w]) DFS(w);

}}

DFS Traversal – Graph To DFS Traversal – Graph To TraverseTraverse

0

1 2

3 4 5 6

7

1

2

3

4

5

6

7

0 1 2

0 3 4

0 5 6

1 7

1 7

2 7

2 7

3 4 5 6

DFS TraversalDFS Traversal

0

1 2

3 4 5 6

7

Order of Traversal:

0, 1, 3 7, 4, 5, 2, 6

Note that performing DFScan find connected components. In this case,the whole graph is connectedand thus all nodes were visited.

Analysis of DFS – From a Analysis of DFS – From a single nodesingle node

Running time dependent on graph Running time dependent on graph structure and representationstructure and representation If graph G is represented by adjacency listsIf graph G is represented by adjacency lists

Determine vertices adjacent to vertex v by Determine vertices adjacent to vertex v by following chain of links.following chain of links.

Each node in each lists is visited at most once, Each node in each lists is visited at most once, and there are 2*e list nodes. Thus, the running and there are 2*e list nodes. Thus, the running time is bounded by the number of edges.time is bounded by the number of edges.

If graph G is represented by an adjacency If graph G is represented by an adjacency matrixmatrix Have to look at n items (n = number of vertices) Have to look at n items (n = number of vertices)

for each vertex (scanning down each row in the for each vertex (scanning down each row in the matrix)matrix)

Running time is O(n*n).Running time is O(n*n).

Breadth First TraversalBreadth First Traversal

Similar to descending across levels Similar to descending across levels of a binary tree.of a binary tree.

Visit the starting vertex v.Visit the starting vertex v. Visit all unvisited vertices directly Visit all unvisited vertices directly

adjacent to v.adjacent to v. Recurse, visiting all unvisited Recurse, visiting all unvisited

vertices directly adjacent to those vertices directly adjacent to those from the previous step.from the previous step.

BFS Traversal AlgorithmBFS Traversal Algorithmvoid Graph::BFS(int v)void Graph::BFS(int v){{

visited = new bool[n];visited = new bool[n];for (int i = 0; i < n; i++) visited[i] = false;for (int i = 0; i < n; i++) visited[i] = false;visited[v] = true;visited[v] = true;Queue<int> q;Queue<int> q;q.insert(v);q.insert(v);while (!q.isEmpty()) {while (!q.isEmpty()) {

v = *q.Delete(v);v = *q.Delete(v);for (all vertices w adjacent to v)for (all vertices w adjacent to v)

if (!visited[w]) {if (!visited[w]) {q.insert(w);q.insert(w);visited[w] = true;visited[w] = true;

}}}}

delete [] visited;delete [] visited;}}

BFS Traversal – Graph to BFS Traversal – Graph to TraverseTraverse

0

1 2

3 4 5 6

7

1

2

3

4

5

6

7

0 1 2

0 3 4

0 5 6

1 7

1 7

2 7

2 7

3 4 5 6

BFS TraversalBFS Traversal

0

1 2

3 4 5 6

7

Order of Traversal:

0, 1, 2, 3, 4, 5, 6, 7

Note that performing BFScan find connected components. In this case,the whole graph is connectedand thus all nodes were visited.

Analysis of BFSAnalysis of BFS

Each visited vertex enters queue exactly Each visited vertex enters queue exactly once (n vertices).once (n vertices).

Once in the queue, have to review list of Once in the queue, have to review list of neighbors.neighbors. For adjacency matrix, that list is n items long, For adjacency matrix, that list is n items long,

meaning the total time is O(n*n)meaning the total time is O(n*n) For adjacency list, that list has degree(vertex) For adjacency list, that list has degree(vertex)

items, and the sum of the degrees for all n items, and the sum of the degrees for all n vertices is O(e), so total cost is bounded by vertices is O(e), so total cost is bounded by number of edgesnumber of edges

Same cost as DFSSame cost as DFS

Connected ComponentsConnected Components

How do we find all connected How do we find all connected components?components? Calling BFS or DFS will find a Calling BFS or DFS will find a

connected component connected component Those vertices connected to the start nodeThose vertices connected to the start node

To find all connected components,To find all connected components, Select a start vertex, call DFSSelect a start vertex, call DFS Select another start vertex which hasn’t Select another start vertex which hasn’t

been visited by a previous DFS, call DFSbeen visited by a previous DFS, call DFS Repeat until all vertices have been visited.Repeat until all vertices have been visited.

Connected Components Connected Components AlgorithmAlgorithm

void Graph::Components()void Graph::Components()

{{

visited = new bool[n];visited = new bool[n];

for (int i = 0; i < n; i++) visited = false;for (int i = 0; i < n; i++) visited = false;

for (i = 0; i < n; i++)for (i = 0; i < n; i++)

{{

if (!visited[i]) { DFS(i); outputComponent(); }if (!visited[i]) { DFS(i); outputComponent(); }

}}

delete [] visited;delete [] visited;

}}

Analysis of Connected Analysis of Connected ComponentsComponents

Using adjacency lists,Using adjacency lists, Any call to DFS is O(e’) where e’ is the set of Any call to DFS is O(e’) where e’ is the set of

edges present in the particular connect edges present in the particular connect component the start node is in.component the start node is in.

Sum over e’ has to equal |E|, the total number of edgesSum over e’ has to equal |E|, the total number of edges For loop itself takes O(n) time and calls DFS for For loop itself takes O(n) time and calls DFS for

non-visited nodesnon-visited nodes DFS not called every time as mark many nodes with each DFS not called every time as mark many nodes with each

DFSDFS Total time: O(n+e)Total time: O(n+e)

With adjacency matrix, O(n*n) [have to look With adjacency matrix, O(n*n) [have to look at all columns for all rows]at all columns for all rows]

Spanning TreesSpanning Trees

If a graph G is connected, a DFS or BFS If a graph G is connected, a DFS or BFS starting at any vertex visits all vertices in starting at any vertex visits all vertices in G.G.

There are a set of particular edges that There are a set of particular edges that are traversed during this process.are traversed during this process.

Let T be the set of edges that are Let T be the set of edges that are traversed, and N be the remaining edges.traversed, and N be the remaining edges.

BFS TraversalBFS Traversal

0

1 2

3 4 5 6

7

Order of Traversal:

0, 1, 2, 3, 4, 5, 6, 7

Edges traversed [T]: (0,1), (0,2), (1,3), (1,4), (2,5), (2,6)(3,7)

Not traversed[N]:(4,7), (5,7), (6,7)

A Spanning Tree A Spanning Tree AlgorithmAlgorithm

Not hard to record traversed edges in BFS Not hard to record traversed edges in BFS (or DFS) – add to a dynamic list whenever (or DFS) – add to a dynamic list whenever encounter new edge.encounter new edge.

void Graph::BFS(int v)void Graph::BFS(int v){{

visited = new bool[n];visited = new bool[n];LinkedList tEdgeList = new LinkedList();LinkedList tEdgeList = new LinkedList();for (int i = 0; i < n; i++) visited[i] = false;for (int i = 0; i < n; i++) visited[i] = false;visited[v] = true;visited[v] = true;Queue<int> q;Queue<int> q;q.insert(v);q.insert(v);while (!q.isEmpty()) {while (!q.isEmpty()) {

v = *q.Delete(v);v = *q.Delete(v);for (all vertices w adjacent to v)for (all vertices w adjacent to v)

if (!visited[w]) {if (!visited[w]) {q.insert(w);q.insert(w);tEdgeList.append(v,w);tEdgeList.append(v,w);visited[w] = true; } }visited[w] = true; } }

delete [] visited;delete [] visited;}}

Spanning TreesSpanning Trees This set of edges is called a This set of edges is called a spanning treespanning tree for the for the

graph.graph.

A spanning tree is any tree that consists solely of A spanning tree is any tree that consists solely of edges from G and that includes all vertices in G (the edges from G and that includes all vertices in G (the tree “spans” all vertices of the graph).tree “spans” all vertices of the graph).

Spanning trees are not unique.Spanning trees are not unique. Generated from DFS – called depth-first spanning treeGenerated from DFS – called depth-first spanning tree Generated from BFS – called breadth-first spanning Generated from BFS – called breadth-first spanning

treetree

BFS Spanning TreeBFS Spanning Tree0

1 2

3 4 5 6

7

Order of Traversal:0, 1, 2, 3, 4, 5, 6, 7

0

1 2

3 4 5 6

7

Edges traversed [T]: (0,1), (0,2), (1,3), (1,4), (2,5), (2,6)(3,7)

BFS Spanning Tree

DFS Spanning TreeDFS Spanning Tree0

1 2

3 4 5 6

7

Order of Traversal:0, 1, 3 7, 4, 5, 2, 6

0

1 2

3 4 5 6

7

DFS Spanning Tree

Edges traversed [T]: (0,1), (1,3), (3,7), (7,4), (7,5), (5,2), (2,6)

Spanning TreesSpanning Trees Spanning tree is a minimal subgraph G’ of G Spanning tree is a minimal subgraph G’ of G

such that V(G’) = V(G) and G’ is connectedsuch that V(G’) = V(G) and G’ is connected Given all vertices from original graph, it is the smallest Given all vertices from original graph, it is the smallest

set of edges one needs for the graph to be connected set of edges one needs for the graph to be connected (to be able to get from any one node to any other)(to be able to get from any one node to any other)

Any connected graph with n vertices must have Any connected graph with n vertices must have n-1 edges or more.n-1 edges or more. Minimally, every vertex has incoming and outgoing (2n) Minimally, every vertex has incoming and outgoing (2n)

except last and first (-2) => 2n-2. Every edge is except last and first (-2) => 2n-2. Every edge is counted twice (as its outgoing and incoming) so divide counted twice (as its outgoing and incoming) so divide by 2 => [(2n-2)/2] = n-1by 2 => [(2n-2)/2] = n-1

Thus, a spanning tree has exactly n-1 edges (it is Thus, a spanning tree has exactly n-1 edges (it is the minimal subgraph of G that is connected).the minimal subgraph of G that is connected).

Applications of Spanning Applications of Spanning TreesTrees

Very useful in determining optimal connectionsVery useful in determining optimal connections Example: Communication networks (laying out Example: Communication networks (laying out

cable): Assume vertices are cities and edges are cable): Assume vertices are cities and edges are communication links between cities.communication links between cities.

All possible spanning trees are all the possible All possible spanning trees are all the possible ways you could set up communication links so ways you could set up communication links so that everyone could talk to everyone else.that everyone could talk to everyone else.

Usually interested in finding the cheapest set of Usually interested in finding the cheapest set of links: Requires setting weights (costs) on links links: Requires setting weights (costs) on links and finding “minimum spanning tree” [will see and finding “minimum spanning tree” [will see algorithm soon!]algorithm soon!]

Interesting problem – there are potentially many Interesting problem – there are potentially many spanning trees? How do you find the minimal spanning trees? How do you find the minimal efficiently (can’t look at them all!)efficiently (can’t look at them all!)

Finding Minimum CostFinding Minimum CostSpanning TreesSpanning Trees

Given a weighted directed graph and a Given a weighted directed graph and a spanning tree for the graph, define the spanning tree for the graph, define the costcost of the spanning tree as the sum of of the spanning tree as the sum of the weights of the trees’ edges.the weights of the trees’ edges.

The minimum cost spanning tree is the The minimum cost spanning tree is the tree with minimal cost (the smallest sum tree with minimal cost (the smallest sum over edge weights).over edge weights).

3 different efficient algorithms for finding 3 different efficient algorithms for finding minimum spanning treeminimum spanning tree

Greedy AlgorithmsGreedy Algorithms

All 3 algorithms are greedy:All 3 algorithms are greedy: Work in stagesWork in stages From the set of feasible decisions, make the best From the set of feasible decisions, make the best

decision possible (given some metric of best) for decision possible (given some metric of best) for current stage. current stage.

Can’t change mind laterCan’t change mind later Feasible ensures that the solution will obey problem Feasible ensures that the solution will obey problem

constraintsconstraints Repeat for rest of stages, given the decisions you Repeat for rest of stages, given the decisions you

have already made and what’s left to do.have already made and what’s left to do.

BestBest is usually defined as is usually defined as least cost or highest least cost or highest profitprofit..

Useful in many other programming domainsUseful in many other programming domains