21
1 Graphs and Search Trees Instructor: Mainak Chaudhuri [email protected]

1 Graphs and Search Trees Instructor: Mainak Chaudhuri [email protected]

Embed Size (px)

Citation preview

Page 1: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

1

Graphs and Search Trees

Instructor: Mainak [email protected]

Page 2: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

2

Building a graphclass VertexList { private int vertexId; private VertexList next;

public VertexList (int x) { vertexId = x; next = null; } // next slide

Page 3: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

3

Building a graph public VertexList Add (int x) { // Add at the front VertexList newHead = new

VertexList (x); newHead.SetNext(this); return newHead; } public void SetNext (VertexList vl) { next = vl; } // next slide

Page 4: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

4

Building a graph public void Print () { if (next==null) { System.out.println (vertexId); } else { System.out.print (vertexId + “,

”); next.Print(); } }} // end class

Page 5: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

5

Building a graphclass GraphBuilder { public static void main (String a[])

throws java.io.IOException { // Array of vertex lists is a graph VertexList graph[]; char c; boolean directed = false; int vertex1, vertex2, n, i; MyInput inp = new MyInput(); // next slide

Page 6: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

6

Building a graph System.out.print (“Enter number of

vertices:”); n = inp.ReadInt(); graph = new VertexList[n]; for (i=0; i<n; i++) { graph[i] = null; } // next slide

Page 7: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

7

Building a graph System.out.print (“Directed or

undirected? (d/u)”); c = inp.ReadChar(); if (c==‘d’) { directed = true; } c = inp.ReadChar();// eat the line

feed while (true) { System.out.print (“Enter an edge

as two vertex ids: ”); // next slide

Page 8: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

8

Building a graph vertex1 = inp.ReadInt(); vertex2 = inp.ReadInt(); if (graph[vertex1] != null) { graph[vertex1] =

graph[vertex1].Add (vertex2); } else { graph[vertex1] = new

VertexList (vertex2); } // next slide

Page 9: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

9

Building a graph if (directed==false) { // Need to add vertex2->vertex1 if (graph[vertex2] != null) { graph[vertex2] =

graph[vertex2].Add (vertex1); } else { graph[vertex2] = new VertexList

(vertex1); } } // next slide

Page 10: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

10

Building a graph System.out.print (“More edges?

(y/n)”); c = inp.ReadChar(); if (c==‘n’) { break; } else { c = inp.ReadChar(); // line feed } } // end of while // next slide

Page 11: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

11

Building a graph // Print out the input graph for (i=0; i<n; i++) { System.out.println (“Neighbours

of vertex ” + i + “:”); graph[i].Print(); } } // end main} // end class

Page 12: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

12

Binary search trees• Recall that on n vertices, the minimum

depth is O(log n)– You can achieve this if you have a balanced

binary search tree (this is possible to ensure in O(log n) time, but we will not discuss)

– The maximum depth is still O(n)• Example: insertion in sorted order

• Worst case search time (i.e. the number of comparisons) in a binary search tree is equal to the depth of the tree– This is O(log n) for nearly balanced trees

• Next few slides builds a binary search tree, searches in a binary search tree, and prints the values in sorted order

Page 13: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

13

Binary search treesclass BSTVertex { private int value; private BSTVertex left; private BSTVertex right;

public BSTVertex (int x) { value = x; left = null; right = null; } // next slide

Page 14: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

14

Binary search trees public void Insert (int x) { // this points to root of a subtree if (x <= value) { if (left == null) { left = new BSTVertex (x); } else { left.Insert (x); } } // next slide

Page 15: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

15

Binary search trees else { if (right == null) { right = new BSTVertex (x); } else { right.Insert (x); } } } // next slide

Page 16: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

16

Binary search trees public boolean Search (int x) { if (x == value) { return true; } else if (x < value) { if (left == null) { return false; } else { return left.Search (x); } } // next slide

Page 17: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

17

Binary search trees else { if (right == null) { return false; } else { return right.Search (x); } } } // next slide

Page 18: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

18

Binary search trees public void PrintSorted () { // Known as in-order traversal // Prints in ascending order of values if (left != null) { left.PrintSorted (); } System.out.print (value + “ ”); if (right != null) { right.PrintSorted (); } }} // end class

Page 19: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

19

Binary search treesclass BSTBuilder { public static void main (String a[])

throws java.io.IOException { BSTVertex root = null; int x; char c; MyInput inp = new MyInput (); while (true) { System.out.print (“Enter the next

value: ”); x = inp.ReadInt(); // next slide

Page 20: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

20

Binary search trees if (root != null) { root.Insert (x); } else { root = new BSTVertex (x); } System.out.print (“More values?

(y/n)”); c = inp.ReadChar (); // next slide

Page 21: 1 Graphs and Search Trees Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

21

Binary search trees if (c==‘n’) { break; } else { c = inp.ReadChar (); // line feed } } // end while // Print out the tree root.PrintSorted (); System.out.print (“\n”); } // end main} // end class