






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Simple path, directed and undirected graph, connected graph, A complete graph and digraph are key points of this lecture
Typology: Lecture notes
1 / 11
This page cannot be seen from the preview
Don't miss anything!







a particular property. For example, find a path with the minimum length from the root to a given vertex (node)
o Simple path: a path in which all vertices, except possibly the first and last, are different o Undirected graph : a graph whose vertices do not specify a specific direction o Directed graph : a graph whose vertices do specify a specific direction o Connected graph : there is at least one path between every pair of vertices o Bipartite graphs : graphs that have vertexes that are partitioned into 2 subsets A and B, where every edge has one endpoint in subset A and the other endpoint in subset B o A complete graph : an n-vertex undirected graph with n(n-1)/2 edges is a complete graph o A complete digraph : (denoted as Kn) for n-vertices a complete digraph contains exactly n(n-1) directed edges o Incident: the edge (i, j) is incident on the vertices i and j (there is a path between i and j ) o In-degree : the in-degree d of vertex i is the # of edges incident to i (the # of edges coming into this vertex) o The out-degree: the out-degree d of vertex i is the # of edges incident from vertex i (the # of edges leaving vertex i ) o The degree of a vertex : the degree d of vertex i of an undirected graph is the number of edges incident on vertex i o Connected component : a maximal sub-graph that is connected, but you cannot add vertices and edges from the original graph and retain connectedness. A connected graph has EXACTLY one component o Communication network : Each edge is a feasible link that can be constructed. Find the components and create a small number of feasible links so that the resulting network is connected o Cycles : the removal of an edge that is on a cycle does not affect the networks connectedness (a cycle creates a sort of loop between certain vertices, for example there is a path that links vertex a to b to c and then back to a )
When creating the matrix ask yourself the following: o Does 1 have a direct path to 1? No, so enter a zero o Does 1 have a direct path to 2? Yes, so enter a one o Does 1 have a direct path to 3? No, so enter a zero o Does 1 have a direct path to 4? Yes, so enter a one o Does 1 have a direct path to 5? No, so enter a zero o Does 2 have a direct path to 1? Yes, so enter a one o Does 2 have a direct path to 2? No, so enter a zero o Does 2 have a direct path to 3? No, so enter a zero o Does 2 have a direct path to 4? No, so enter a zero o Does 2 have a direct path to 5? Yes, so enter a one o And the process continues for each vertex (node) o If this were a weighted graph the we would store null instead of zero, and the weight of the edge instead of 1
Vertex
Edge
o A search method starts at a given vertex v and visits/labels/marks every vertex that is reachable from v o Many graph problems are solved using search methods
Page 2
public int newDFS(int theV, int theK) { if( theK <= 0 || theK > vertices()) { //no vertex to return return 0; } else { //initialize static variables count = 0; targetCount = theK; label = new boolean[vertices() + 1]; //the default value is false
return newDFS(theV); //making the call to the static method newDFS(theV) } }
/** the private method that does the actual depth-first traversal */ private static int newDFS(int v) { //return vertex if found //reached a new vertex count++; if (count == targetCount) { return v: //we found the vertex we were looking for }
else //we need to reach more vertices { label[v] = true; Iterator iv = iterator(v);
while(iv.hasNext()) { //visit an adjacent vertex of v int u = ((EdgeNode.iv.next()).vertex; if(!label[u]) { //u is a new vertex int w = newDFS(u);
if(w>0) //the target vertex has been found { return w; }
The greedy single source single destination algorithm: o Leave the source vertex using the cheapest/shortest edge. o Leave the new vertex using the cheapest edge subject to the constraint that a new vertex is reached o Continue until the destination is reached.
The greedy single source all destination algorithm: o Let d( i ) (distanceFromSource( i )) be the length of the shortest one edge extension of an already generated shortest path, the one edge extension ends at i. o The next shortest path is to an as yet un-reached vertex for which d( ) is the least. o Let p( i ) (predecessor( i )) be the vertex just before vertex i on the shortest one edge extension of i o Terminate the algorithm as soon as the shortest path to the desired vertex has been generated.
Page 4
Start with an empty set T of edges. while (E is not empty && |T| != n-1) { Let (u,v) be a least-cost edge in E. E = E - {(u,v)}. // Delete edge from E if ((u,v) does not create a cycle in T) Add edge (u,v) to T. } if (| T | == n-1) T is a min-cost spanning tree. else Network has no spanning tree.
Data structures for Kruskal’s method : o Use an array (not an ArrayLinearList) to store a set T of selected edges o Questions to ask yourself about the set T : É Does T have n-1 edges where n is the number of nodes in the tree? Check the size of the array to determine this. É Does the addition of edge (u, v) to T result in a cycle? This can be hard to determine. É If not then add the edge to T at the end of the array. o Each component of T is defined by the vertices in the component o We will represent each component as a set of vertices: {1, 2, 3, 4} {5, 6} {7, 8} o Two vertices are in the same component iff they are in the same set of vertices , so 2 and 3 are in the same component but 4 and 6 are not so the addition of an edge between 4 and 6 will not result in a cycle. o When an edge (u, v) is added to T, the two components that have vertices (u, v) are combined to form a single component. So with the addition of the edge between 4 and 6 above will result in the components that originally contained {1, 2, 3, 4} + {5, 6} ==> {1, 2, 3, 4, 5, 6} o Use min-heap operations to get the edges into increasing order of cost. Time complexity O(e log e) o The criteria for determining if (u, v) is in the same component is as follows: S1 = find(u); //use FastUnionFind S2 = find(v);
If (S1 != S2) union(S1, S2); Prim’s method: Start with a 1-vertex tree and grow it into an n-vertex tree by repeatedly adding a vertex and an edge (if we have a choice then add the least costly edge ) ( See page 10 for an example). This method is the fastest Sollin’s method: Start with an n-vertex forest. Each component selects a least-cost edge to connect to another component. Eliminate duplicate selections and possible cycles. This process is repeated until there is only 1 component left. Example given below This one is chosen first: because it has the least cost 2 8 This one is last 2 8
4 6 12 7 This one is 3rd 7 This is chosen 2nd^4
^ Page 5 Edge rejection greedy strategy : Starting with a connected graph, repeatedly find a cycle and eliminate the highest cost edge on this cycle, stop when no cycle remains Considering the edges in descending order of cost, eliminate an edge provided this leaves behind a connected graph.
Divide the large instance into smaller instances (performance is best when the smaller instances that result from the divide step are approximately the same size) Solve the smaller instances somehow, such as continuing to break them into smaller instances until there is only one element per instance. For Example: Please see the diagram below : imagine a tree where the root is the original problem and consists of 10 integers in random order, we wish to sort these integers into decreasing order. We first break up the root into sub-trees that contain portions of the root, now we break up these sub-trees into even smaller groups of integers until we finally reach the leaves, which have only single integer. At this time we compare two leaves and sort them accordingly. Array [2, 4, 1, 19, 22, 33, 42, 61, 8, 7]
Now compare 2, 4, 1 19, 22 33, 42, 61 8, 7 [4, 2] to [1] and store in the root 2, 4 1 19 22 33, 42 61 8 7 [4, 2]
4 is larger than 2 so we store [4, 2] in the root
Now that we have broken the problem down into much smaller problems we compare the leaves and place them into their root in the proper order. After the divide and conquer merge sort terminates the array would be as follows: Array [61, 42, 33, 22, 19, 8, 7, 2, 2, 1]
Combine the results of the smaller instances to obtain the results of the larger instance The working of a recursive divide-and-conquer algorithm can be described as a tree – a recursion tree The algorithm moves down the recursion tree dividing large instances into smaller ones The leaves represent small instances The recursive algorithm moves back up the tree combining the results from the sub-trees
É The initially sorted segments are the naturally occurring sorted segments in the input
4 5 4
1 2 3 1
5
2 3
Know Kruskal’s, Dijkstra’s, Prims, and Sollin’s methods, there is sure to be a question involving these (not so much
the coding but focus on the concepts) Completely understand and be able to reproduce Depth-first-search and Breadth-first-search, problems involving these are certain to be on the exam since they can be used to traverse all graphs. Be able to reproduce from memory all the code for both BinaryTreeNode and LinkedBinaryTree (please
see page 5 and pages 15 through 18 on “Notes for Exam II”) pay special attention to the height method of LinkedBinaryTree Know everything there is to know about AVL, HBLT, Binary Search, winner, and loser trees, coding problems
involving these may be on the exam (this is why knowing the height method from memory is very useful). For example: write code that tests the balance of an indexed AVL tree and can make adjustments to bring the tree into balance (look at page 605 in the text for information on insertions into an AVL tree) Do the sample exams found on the texts web site, time yourself to make sure that you understand the material well enough to complete it in 75 minutes, then check your answers and give yourself a grade. If you don’t score well then study the stuff you messed up on (this is how I judge my knowledge of the material and works for me). GOOD LUCK ON THE EXAM! Page 7
We may perform a breadth-first or depth-first search in each component of the graph. In each component, the first vertex that is visited may be assigned to either set. The assignment of the remaining vertices in the component is forced by the bipartite labeling requirement that the end points of an edge have different labels. The code below assigns the first vertex in each component the label 1. It is a modified version of the code for breadth first search.
/** Label the vertices such that every edge connects
int n = vertices();
// create label array, default initial values are 0 int [] label = new int [n + 1];
// do a breadth first search in each component ArrayQueue q = new ArrayQueue(10); for (int v = 1; v <= n; v++) if (label[v] == 0) {// new component, label the vertices in this component label[v] = 1; q.put(new Integer(v)); while (!q.isEmpty()) { // remove a labeled vertex from the queue int w = ((Integer) q.remove()).intValue();
// mark all unreached vertices adjacent from w Iterator iw = iterator(w); while (iw.hasNext()) {// visit an adjacent vertex of w int u = ((EdgeNode) iw.next()).vertex; if (label[u] == 0) {// u is an unreached vertex q.put(new Integer(u)); label[u] = 3 - label[w]; // assign u the other label } else // u already labeled if (label[u] == label[w]) // both ends of edge assigned same label return null; } } }
// labeling successful return label; }
When the graph is bipartite, each vertex of the graph is added to the queue exactly once. Each vertex is also deleted from the queue exactly once. When a vertex is deleted from the queue, all vertices adjacent to it are examined. It takes Theta(n) time to find and examine the vertices adjacent to vertex w when an adjacency matrix is used and Theta(dw) when adjacency lists are used. If the algorithm does not terminate early because of an exception or because the graph is not bipartite, all vertices in the graph get examined. The total examination time is Theta(n^2 ) when adjacency matrices are used and Theta(e) when adjacency lists are used. The remainder of the algorithm takes O(n) time. Allowing for exceptions and the fact that the algorithm may terminate early because the graph is not bipartite, we see that the overall complexity is O(n^2 ) when adjacency matrices are used and O(n+e) when adjacency lists are used.
A test program, input, and output appear in the files TestBipartite.*.
Page 8
breadthFirstSearch( v ) { Label vertex v as reached. Initialize Q to be a queue with only v in it. While( Q is not empty) { delete a vertex w from the queue. Let u be a vertex (if any) adjacent from w. While( u ) { if( u has not been labeled) { add u to the queue. Label u as reached. } u = next vertex that is adjacent from w. } } }
/** breadth-first search
while (!q.isEmpty()) { //remove a labeled vertex from the queue int w = ((Integer) q.remove()).intValue();
//mark all unreached vertices adjacent from w Iterator iw = iterator(w); While(iw.hasNext()) { //visit an adjacent vertex of w int u = ((EdgeNode) iw.next()).vertex;
A complete digraph digraph bipartite graph complete graph
A digraph is strongly connected iff it contains a directed path from i to j and from j to i for every pair of distinct vertices i and j.
Page 10 12 1 3 1 2 3 4
The weighted graph above is used in the next 3 examples Example: kruskal’s method: construct a min cost spanning tree (we end up with n – 1 edges)
1 2 3 4
5 6 7 8
1 2 3 4
5 6 7 8
Example: Prim’s method: construct a min cost spanning tree (assuming we were instructed to start at vertex # 1)
1 1 1 1 1 3 1 2 3
5 5 6 5 6 7 5 6 7 5 6 7
1 2 3 4 1 2 3 4
5 6 7 5 6 7 8 Example: linked adjacency representation: each adjacency list is a chain; there are 2e node in an undirected graph and e node in a directed graph. Each object is represented as an array of chains where each chain is of type Chain.
This one is chosen first because is has the least cost = 1 (ties are broken arbitrarily)
This one is chosen next because is has the 2nd least cost = 1
We start with an n vertex forest, consider edges in ascending order of cost and don’ t create cycles
This one is chosen 3rd^ its cost = 2
This one is chosen 4th its cost = 3
This one is chosen 5th its cost = 3
This one is chosen 6th its cost = 4
This one is chosen 7th its cost = 5
1 12
6 8 5 4
6 4 3 1
3 3
1 4
5 2
6 3
7 9
7 1 4 3
8 5 7 5
6 2
7 3
3 1
2 4
4 5
4 5
3 4
8 9
1 8
Vertex (node) number
Weight of edge