Académique Documents
Professionnel Documents
Culture Documents
Graph Representation
Graphs
Definition of Graphs and Related Concepts Representation of Graphs The Graph Class Graph Traversal
Tuesday, May 01, 2012 Data Structure (CSC312)
Definition of Graphs
A graph is a finite set of nodes with edges between nodes Formally, a graph G is a structure (V,E) consisting of
a finite set V called the set of nodes, and a set E that is a subset of VxV. That is, E is a set of pairs of the form (x,y) where x and y are nodes in V
Tuesday, May 01, 2012 Data Structure (CSC312)
Examples of Graphs
V={0,1,2,3,4} E={(0,1), (1,2), (0,3), (3,0), (2,2), (4,3)}
0 1
2
When (x,y) is an edge, we say that x is adjacent to y, and y is adjacent from x. 0 is adjacent to 1. 1 is not adjacent to 0. 2 is adjacent from 1.
4
3
Tuesday, May 01, 2012
Tom
Tuesday, May 01, 2012
Paul
Data Structure (CSC312)
Graph Representation
For graphs to be computationally useful, they have to be conveniently represented in programs There are two computer representations of graphs:
Adjacency matrix representation Adjacency lists representation
Tuesday, May 01, 2012 Data Structure (CSC312)
1
2
4
3
Paul 5
Cons:
No matter how few edges the graph has, the matrix takes O(n2) in memory
Tuesday, May 01, 2012 Data Structure (CSC312)
Cons:
It can take up to O(n) time to determine if a pair of nodes (i,j) is an edge: one would have to search the linked list L[i], which takes time proportional to the length of L[i].
Tuesday, May 01, 2012 Data Structure (CSC312)
bool Graph::isEdge(int i, int j){ assert(i>=0 && j>=0); return p[i][j] != 0; }; void Graph:;setEdge(int i, int j, datatype x){ assert(i>=0 && j>=0); p[i][j]=x; };
Tom
Tuesday, May 01, 2012
Adjacency Lists:
L[i] is the linked list containing all the neighbors of i
Tuesday, May 01, 2012 Data Structure (CSC312)
Example of Representations
Linked Lists: L[0]: 1, 2, 3 L[1]: 0, 2, 3 L[2]: 0, 1, 3 L[3]: 0, 1, 2 L[4]: 5 L[5]: 4
Tuesday, May 01, 2012
Mary 0
Helen 1
Joe 3 Paul 5
Paths
A path in a graph G is a sequence of nodes x1, x2, ,xk, such that there is an edge from each node the next one in the sequence For example, in the first example graph, the sequence 3, 0, 1, 2 is a path, but the sequence 0, 3, 4 is not a path because (0,3) is not an edge In the sibling-of graph, the sequence John,
Mary, Joe, Helen is a path, but the sequence Helen, Tom, Paul is not a path
Tuesday, May 01, 2012 Data Structure (CSC312)
Graph Connectivity
An undirected graph is said to be connected if there is a path between every pair of nodes. Otherwise, the graph is disconnected Informally, an undirected graph is connected if it hangs in one piece
Disconnected
Tuesday, May 01, 2012 Data Structure (CSC312)
Connected
Connected Components
If an undirected graph is not connected, then each piece is called a connected component.
A piece in itself is connected, but if you bring any other node to it from the graph, it is no longer connected
If the graph is connected, then the whole graph is one single connected component Of Interest: Given any undirected graph G,
Is G connected? If not, find its connected components.
Depth-First Search
DFS follows the following rules:
1. Select an unvisited node x, visit it, and treat as the current node 2. Find an unvisited neighbor of the current node, visit it, and make it the new current node; 3. If the current node has no unvisited neighbors, backtrack to the its parent, and make that parent the new current node; 4. Repeat steps 3 and 4 until no more nodes can be visited. 5. If there are still unvisited nodes, repeat from step 1.
Tuesday, May 01, 2012 Data Structure (CSC312)
The following is an example of the search tree. It is a series of interconnected nodes that we will be searching through: All paths go only from top to bottom. In other words, A has a path to B and C, but B and C do not have a path to A. It is basically like a one-way street. Each lettered circle in our graph is a node. A node can be connected to other via our edge/path, and those nodes that its connects to are called neighbors. B and C are neighbors of A. E and D are neighbors of B, and B is not a neighbors of D or E because B cannot be reached using either D or E.
Tuesday, May 01, 2012 Data Structure (CSC312)
Illustration of DFS
let's us see how the above representation of our graph to find out how Depth First Search works.
Depth first search works by taking a node, checking its neighbors, expanding the first node it finds among the neighbors, checking if that expanded node is our destination, and if not, continue exploring more nodes. Using our same search tree, let's find a path between nodes A and F:
Tuesday, May 01, 2012 Data Structure (CSC312)
Step 0 Let's start with our root/goal node: Let use two lists to keep track of what we are doing - an Open list and a Closed List. An Open list keeps track of what we need to do, and the Closed List keeps track of what we have already done. Open List: A Closed List: <empty>
Tuesday, May 01, 2012 Data Structure (CSC312)
Step 1 Now, let's explore the neighbors of A node. Node A's neighbors are the B and C nodes. Because we are now done with A node, we can remove it from Open list and add it to Closed List. Add those two nodes to Open list. Our current Open and Closed Lists contain the following data: Open List: B, C Closed List: A
Tuesday, May 01, 2012 Data Structure (CSC312)
Step 2 Our Open list contains two items. For depth first search and breadth first search, you always explore the first item from our Open list. The first item in our Open list is the B node. B is not our destination, so let's explore its neighbors: node B is expanded now, lets to remove it from the Open list and add it to the Closed List. Our new nodes are D and E, and we add these nodes to the beginning of our Open list: Open List: D, E, C Closed List: A, B
Tuesday, May 01, 2012 Data Structure (CSC312)
Step 3 Because D is at the beginning of the Open List, we expand it. D isn't our destination, and it does not contain any neighbors. All we need to do in this step is to remove D from our Open List and add it to our Closed List: Open List: E, C Closed List: A, B, D
Tuesday, May 01, 2012 Data Structure (CSC312)
Step 4 We now expand the E node from our Open list. E is not our destination, so we explore its neighbors and find out that it contains the neighbors F and G. Remember, F is our target. Despite F being on our path, we only end when we are about to expand our target Node - F in this case: Our Open list will have the E node removed and the F and G nodes added. The removed E node will be added to our Closed List: Open List: F, G, C Closed List: A, B, D, E
Step 5 We now expand the F node. Since it is our intended destination, we stop: We remove F from our Open list and add it to our Closed List. Since we are at our destination, there is no need to expand F in order to find its neighbors. Our final Open and Closed Lists contain the following data: Open List: G, C Closed List: A, B, D, E, F
Tuesday, May 01, 2012 Data Structure (CSC312)
The final path taken by our depth first search method is what the final value of our Closed List is: A, B, D, E, F.
Tuesday, May 01, 2012 Data Structure (CSC312)
// Put this before dfs() // returns the leftmost unvisited int t=S.peek( ); int y=getNextUnvisitedNeighbor( // neighbor of node t. If none t,G,visited,n); // remains, returns n. int getNextUnvisitedNeighbor(int t, if (y<n){ graph G, bool visited[],int n){ visited[y]=true; for (int j=0;j<n;j++) S.push(y); if (G.isEdge(t,j) && !visited[j]) parent[y]=t; return j; } // if no unvisited neighbors left: else S.pop( ); return n; } } //Put this before dfs(). This returns the next unvisited node, or n otherwise int getNextUnvisited(bool visited[],int n, int lastVisited){ int j=lastVisited+1; while (visited[j] && j<n) j++; return j; }
{
Tuesday, May 01, 2012 Data Structure (CSC312)
Breadth-First Search
BFS follows the following rules:
1. Select an unvisited node x, visit it, have it be the root in a BFS tree being formed. Its level is called the current level. 2. From each node z in the current level, in the order in which the level nodes were visited, visit all the unvisited neighbors of z. The newly visited nodes from this level form a new level that becomes the next current level. 3. Repeat step 2 until no more nodes can be visited. 4. If there are still unvisited nodes, repeat from Step 1.
Tuesday, May 01, 2012 Data Structure (CSC312)
Breadth First Search In DFS, newly explored nodes were added to the beginning of the Open list. But in BFS, newly explored nodes are added to the end of the Open list.
Step 0 Let's start with our root/goal node: Open List: A Closed List: <empty>
Step 1 Let's explore the neighbors of the A node. We remove A from our Open list and add A to our Closed List. A's neighbors, the B and C nodes, are added to our Open list. Our current Open and Closed Lists contain the following data: Open List: B, C Closed List: A
Step 2 We take a look at the B node because it appears first in our Open List. Because B isn't our intended destination, we explore its neighbors: B is now moved to our Closed List, but the neighbors of B, nodes D and E are added to the end of our Open list: Open List: C, D, E Closed List: A, B Step 3 We now expand our C node: Since C has no neighbors, we remove C from the Closed List and move on: Open List: D, E Closed List: A, B, C
Tuesday, May 01, 2012 Data Structure (CSC312)
Step 4 Similar to Step 3, we expand node D. Since it isn't our destination, and it too does not have any neighbors, we simply remove D from our to Open list, add D to our Closed List, and continue on: Open List: E Closed List: A, B, C, D
Step 5 Because our Open list only has one item, we have no choice but to take a look at node E. Since node E is our destination, we stop here:
Our final versions of the Open and Closed Lists contain the following data: Open List: <empty> Closed List: A, B, C, D, E
Therefore, traveling from A to E takes you through B, C, and D using breadth first search.
Tuesday, May 01, 2012 Data Structure (CSC312)
Implementation of DFS
Observations:
the first node visited in each level is the first node from which to proceed to visit new nodes.
This suggests that a queue is the proper data structure to remember the order of the steps.
Construct the DFS and BFS by finding a path between nodes A and K