21
Graphs and Tree Decomposition

Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0

Embed Size (px)

Citation preview

Page 1: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0

Graphsand

Tree Decomposition

Page 2: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0

Graph Representations

adjacency matrix

node list edge listA D F

C H

B E G

A B C D E F G HA - 1 1 1 1 1 0 0B 1 - 1 0 1 0 0 1C 1 1 - 1 1 0 0 1D 1 0 1 - 0 1 1 1E 1 1 1 0 - 1 1 0F 1 0 0 1 1 - 1 1G 0 0 0 1 1 1 - 1H 0 1 1 1 0 1 1 -

A - B C D E FB - A C E HC - A B D E HD - A C F G HE - A B C F GF - A D E G HG - D E F HH - B C D F G

A BA CA DA EA FB AB CB EB HC AC BC DC EC HD AD CD FD GD H

E AE BE CE FE GF AF DF EF GF HG DG EG FG HH BH CH DH FH G

node list - lists the nodes connected to each node

edge list - lists each of the edges as a pair of nodes undirected edges may be listed twice XY and YX in order to simplify algorithm implementation

adjacency matrix - for an n-node graph we build an nxn array with 1's indicating edges and 0's no edge the main diagonal of the matrix is unused unless a node has an edge connected to itself. If graph is weighted, 1's are replaced with edge weight values

Page 3: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0

Graph Depth-First Traversal (DFT) Algorithm Implementationtext file representation

Given a graph G(V,E) and a starting node vstart perform a depth-first traversal. Provide a list of the nodes in the order they are traversed.

Graph is defined in a text file of the following format:

1st Line - # of nodes, N # of edges, E2nd Line - List of node labels (you may assume 1 char each)3rd Line - through N+3 Linean N x N adjacency matrix

A D F

C H

B E G

8 A B C D E F G H0 1 1 1 1 1 0 01 0 1 0 1 0 0 11 1 0 1 1 0 0 11 0 1 0 0 1 1 11 1 1 0 0 1 1 01 0 0 1 1 0 1 10 0 0 1 1 1 0 10 1 1 1 0 1 1 0

Page 4: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0

Graph Depth-First Traversal (DFT) Algorithm Implementationpseudo-code

DFT( node vk ){ // deal with current node for each node, vi { if (node_avail(vi)) then DFT(vi) }}

node_avail( vi ) is a Boolean function that returns true if node vi is available for traversal, which

means that vi has not been traversed, and vi is adjacent to vk.

Page 5: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0

Floyd's Algorithm for Shortest Paths

V1

V2V5

V4V3

1

5

1

1

2

6

1

3

2

1 2 3 4 51 0 2 - - -2 3 0 6 - -3 - - 0 - 14 2 - 1 0 15 1 - - - 0

procedure floyd(W,D:matype) isbegin D:=W; for k in 1..n loop for i in 1..n loop for j in 1..n loop D(i,j):=min(D(i,j),D(i,k)+D(k,j)); end loop; end loop; end loop;end floyd;

Floyd's algorithm is very simple to implement. The fact that it works at all is not obvious. Be sure to work through the proof of algorithm correctness in the text.

Page 6: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0

A D F

C H

B E G

Graph Breadth-First Traversal

Given a graph G(V,E) and a starting vertex s, performa breadth-first traversal (BFT) of G. such that eachreachable vertex is entered exactly once.

If all vertices are reachable, the edges traversed andthe set of vertices will represent a spanning treeembedded in the graph G.

1) BFT suggests an iterative process (rather than a recursive one)

2) BFT vertices order of traversal can be maintained using a Queue data structure

3) The preferred representation for the graph is an adjacency matrix

4) We will need a way to keep up with which vertices have been "used" (e.g. a Boolean list)

5) Process begins by placing the starting vertex in the Queue

6) A vertex is taken from the Queue, every unused vertex adj to this vertex is added to the Queue This operation is repeated until the Queue is empty.

8) The output (answer) is returned in the form of a list of vertices in the order they entered the Queue

Page 7: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0

3

0

2

5

6

4

1 0 1 1 0 0 0 01 0 0 1 1 0 01 0 0 1 0 1 00 1 1 0 0 0 10 1 0 0 0 0 10 0 1 0 0 0 10 0 0 1 1 1 0

0123456

0 1 2 3 4 5 6

Breadth-First Traversal of a Graph

There are many applications in computer science for which we need to examine the nodes of a graph (or tree) in some orderly fashion. Previously we have looked at depth-first traversal using a recursive method operating on a stack data structure. Now we will investigate breadth-first traversal using an iterative method operating on a queue data structure.

A graph consisting of nodes and edges connecting pairs of nodes can be represented as an adjacency matrix in which a 1 in row i column j indicates that there is an edge connecting node i to node j. If there is no edge, the corresponding value in the adjacency matrix will be zero (0).

Page 8: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0

import java.util.Scanner;import java.io.*;public class BFT{ public static void main(String[] args) throws Exception { int[][] mat = readMat(); showMat(mat); } public static int[][] readMat() { try { Scanner input = new Scanner(new File("adjmat.txt")); int nrows = input.nextInt(); int ncols = input.nextInt(); int[][] m = new int[nrows][ncols]; for(int i = 0; i < nrows; i++) for(int j = 0; j < ncols; j++) m[i][j] = input.nextInt(); input.close(); return m; } catch(IOException e) { e.printStackTrace(); return null; } }

public static void showMat(int[][] m){ for(int i=0;i<m.length;i++) { for(int j=0;j<m[0].length;j++) System.out.print(m[i][j] + "\t"); System.out.println(); }}

Reading the Adjacency Matrix from a File

Page 9: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0

import java.util.Scanner;import java.io.*;public class BFT{ public static int[][] mat; public static boolean[] avail; public static void main(String[] args) throws Exception { mat = readMat(); avail = new boolean[mat.length]; for(int i=0;i<avail.length;i++) avail[i] = true; showMat(mat); bft(4); }

Supporting Data for BFT Method

Page 10: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0

public static void bft(int n){ int cur; java.util.Queue<Integer> q = new java.util.LinkedList<Integer>(); q.offer(n); avail[n] = false; while(q.size()>0) { cur = q.remove(); System.out.print(cur + " "); for(int k=0;k<mat.length;k++) { if(avail[k] && mat[cur][k]==1) { q.offer(k); avail[k] = false; } } } System.out.println();}

The BFT(n) Method

3

0

2

5

6

4

1

0 1 1 0 0 0 01 0 0 1 1 0 01 0 0 1 0 1 00 1 1 0 0 0 10 1 0 0 0 0 10 0 1 0 0 0 10 0 0 1 1 1 0

0123456

0 1 2 3 4 5 6

Page 11: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0

public class sortTree{ public Node root = null; public sortTree(double val) { root = new Node(val); } public Node add(Node node, double val) { if (node == null) return node = new Node(val); if (val < node.data) node.left = add(node.left, val); else node.right = add(node.right, val); return node; } public void showAll(Node node) { if (node != null) { showAll(node.left); System.out.println(node.data); showAll(node.right); } }

protected class Node { double data; Node left; Node right; public Node(double val) { this(val,null,null); } public Node(double val, Node left_node, Node right_node) { data = val; left = left_node; right = right_node; } }}

sortTree Class

Page 12: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0

public class sortTreeDemo { public static void main(String[] args) { sortTree myTree = new sortTree(Math.random()*1000.0); for(int i=1;i<1000000;i++) myTree.add(myTree.root,Math.random()*1000.0); myTree.showAll(myTree.root); } }

sortTreeDemo

Page 13: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0
Page 14: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0
Page 15: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0
Page 16: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0
Page 17: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0
Page 18: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0
Page 19: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0
Page 20: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0
Page 21: Graphs and Tree Decomposition. Graph Representations adjacency matrix node list edge list ADF CH BEG A B C D E F G H A - 1 1 1 1 1 0 0 B 1 - 1 0 1 0 0