Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 Motivation What are some characteristics of a binary tree?
They have a rigid structure of each node having a single node that points to it (or none, in the case of the root). Sometimes life isn’t so structured. For example: I need to fly to Tokyo. I want to find the cheapest way to fly there. How could I think about the data? For example: I have a set of candy bars in this bag. I want to distribute so everybody gets a kind they like. How do I represent the data? Human graph. If you could pick one person who you would go to for help with an assignment, who would it be? Using your arm as a link, touch that person on the shoulder. What if you could choose two people? For example: I want to travel every bike path in Logan (assuming they had any  ) but I don’t want to ever travel on the same path twice. Can I begin at my home, visit every path, and then return? The data representation in all of these cases is the graph!

3 Terms Graph – set of vertices and set of edges.
More formally, a graph is an ordered pair of finite sets V and E. The set V is the set of vertices. The set E is the set of edges. Sometimes the edges have weight (termed a weighted graph) Sometimes the edges have direction (I can go from A to B but not B to A). A graph with directed edges is termed a digraph or directed graph. Otherwise, we call it an undirected graph. Successor – the follows relationship in a directed graph. Degree – the number of edges incident to or touching a vertex. In-degree – the number of edges coming into a vertex (digraph only). Out-degree – the number of edges going out of a vertex (digraph only). Predecessor – the precedes relationship in a digraph.

4 Draw a graph of five people around you now. The people become the nodes. Let edges be “is born in the same year as you”. Is this directed or undirected? Is in connected? (A connected graph is a graph that you can get from a node to every other node following edges

5 Examples of Graphs “Want to work with”
V={0,1,2,3,4} E={(0,1), (1,2), (0,3), (3,0), (2,2), (4,3)} 1 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. 2 4 3

6 A “Real-life” Example of a Graph
V=set of 6 people: John, Mary, Joe, Helen, Tom, and Paul, of ages 12, 15, 12, 15, 13, and 13, respectively. E ={(x,y) | if x is younger than y} Mary Helen Joe John Tom Paul

7 Intuition Behind Graphs
The nodes represent entities (such as people, cities, computers, words, etc.) Edges (x,y) represent relationships between entities x and y, such as: “x loves y” “x hates y” “x is a friend of y” (note that this not necessarily reciprocal) “x considers y a friend” “x is a child of y” “x is a half-sibling of y” “x is a full-sibling of y” In those examples, each relationship is a different graph

8 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

9 Adjacency Matrix Representation
In this representation, each graph of n nodes is represented by an n x n matrix A, that is, a two-dimensional array A The nodes are (re)-labeled 1,2,…,n A[i][j] = 1 if (i,j) is an edge A[i][j] = 0 if (i,j) is not an edge

10 Example of Adjacency Matrix
1 A = 2 4 3

11 Another Example of Adj. Matrix
Re-label the nodes with numerical labels Mary 0 Helen 1 A = Joe 3 John 2 Tom 4 Paul 5

12 Pros and Cons of Adjacency Matrices
Simple to implement Easy and fast to tell if a pair (i,j) is an edge: simply check if A[i][j] is 1 or 0 Cons: No matter how few edges the graph has, the matrix takes O(n2) in memory

13 Adjacency Lists Representation
A graph of n nodes is represented by a one-dimensional array L of linked lists, where L[i] is the linked list containing all the nodes adjacent from node i. The nodes in the list L[i] are in no particular order

14 Example of Linked Representation
L[0]: empty L[1]: empty L[2]: 0, 1, 4, 5 L[3]: 0, 1, 4, 5 L[4]: 0, 1 L[5]: 0, 1 Helen 1 Mary 0 Joe 3 John 2 Tom 4 Paul 5

15 class EdgeNode { public: int toNode; // Node id of target of edge int fromNode; // Node id of source of edge int distance; // Cost of Edge (for a weighted graph only) EdgeNode *next; // Next edge in linked list for adjacency list representation // Constructor – note the use of default parameters. // Note the strange assignments (distance(dist)) – they aren’t my favorite either, but texts seem to like them EdgeNode( int from = -1, int to=-1, int distance=0, EdgeNode *nextEdge = NULL): distance(dist),toNode( to ), fromNode(from), next(nextEdge){}; }; // EdgeNode

16 class GraphNode { public: inf nodeID; // ID of node; something that makes it easy to find the node in the array string name; // Node name EdgeNode *adj; // Adjacency list bool visited; // true if visited already GraphNode( int id= -1, string nameEle=" ", EdgeNode *adjList = NULL): nodeID(id), name(nameEle), adj(adjList){ visited = false;}  }; // GraphNode class Graph { protected: GraphNode *G; // Array of nodes of graph – will be given space once the size is known int nodeCt; // Size of G public: Graph(int size ) { G = new GraphNode[size]; nodeCt = size;}; // create node array string toString(string label,ostream & fout); void BuildGraph(istream & inf); }; // Graph

17 Directed vs. Undirected Graphs
If the directions of the edges matter, then we show the edge directions, and the graph is called a directed graph (or a digraph) If the relationships represented by the edges are symmetric (such as (x,y) is edge if and only if x is a sibling of y), then we don’t show the directions of the edges, and the graph is called an undirected graph.

18 Examples of Undirected Graphs
V=set of 6 people: John, Mary, Joe, Helen, Tom, and Paul, where the first 4 are siblings, and the last two are siblings E ={(x,y) | x and y are siblings} if (x,y) is an edge: we say that x is adjacent to y, & y adjacent to x. We also say that x and y are neighbors Mary Helen Joe John Tom Paul

19 Representations of Undirected Graphs
The same two representations for directed graphs can be used for undirected graphs Adjacency matrix A: A[i][j]=1 if (i,j) is an edge; 0 otherwise Adjacency Lists: L[i] is the linked list containing all the neighbors of i

20 Example of Representations
Mary 0 Helen 1 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 John 2 Joe 3 Tom 4 Paul 5 Adjacency Matrix: A =

21 Definition of Some Graph Related Concepts
Let G be a directed graph The indegree of a node x in G is the number of edges coming to x The outdegree of x is the number of edges leaving x. Let G be an undirected graph The degree of a node x is the number of edges that have x as one of their end nodes The neighbors of x are the nodes adjacent to x

22 Things for You To Do Add a member function to the class graph, called getIndegree( int x), which returns the indegree of node x Add a member function to the class graph, called getOutdegree( int x), which returns the outdegree of node x

23 Pros and Cons of Adjacency Lists
Saves on space (memory): the representation takes as many memory elements as there are nodes and edge. 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].

24 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

25 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 Connected Disconnected

26 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.

27 Strongly Connected Components
Every pair of vertices are reachable from each other Graph G is strongly connected if, for every u and v in V, there is some path from u to v and some path from v to u. Strongly Connected Not Strongly Connected

28 Example of Strongly Connected Components (SCC)
{ a , c , g } g c { f , d , e , b } d e f b We say a graph is weakly connected if the graph (ignoring directions) is connected

29 Sometimes we shrink a SCC into one “supernode”

30 Suppose the graph below represents one way streets in a town and the nodes represent hotels.
Maybe I only care about hotels 0,1,3,6 in the figure. I could take those nodes and the edges between them and talk about the subgraph. Figure Subgraph – a graph that consists of a subset of vertices and a subset of edges. Normally, the edges are all edges between the vertices and no others.

31 By definition a graph does not contain:
Multiple copies of the same edge. Self-edges – edges of the form . This is also called a loop. If we need multiple edges between nodes, it is termed a multigraph. In our hotel example, multiple edges between 6 and 5 could represent two different road between the hotels. Sometimes we want to talk about a sequence of edges: Path – a sequence of edges (one ends where next begins) Simple path – there are no repeated vertices or edges. Length of a path – the sum of the lengths of the edges on the path. Sometimes we want to talk about cycles (a path that begins and ends at the same place) Simple cycle – a cycle with no repeated vertices (except at the beginning and the end). The cycle (0320) in the graph above is a simple cycle. Connected component – A component is a subgraph in which for every pair of vertices in the component, there exists some path that connects them. Connected graph – there is a path between every pair of vertices in the graph. This implies that a graph with n vertices must have at least edges.

32 Graph Traversal Techniques
The previous connectivity problem, as well as many other graph problems, can be solved using graph traversal techniques There are two standard graph traversal techniques: Depth-First Search (DFS) Breadth-First Search (BFS)

33 Depth First Search Backtrack to 10,6,7 9, 11, 8, 5 Backtrack to 8,11
Output List: 1 2 4 3 7 6 10 13 9 11 8 5 15 14 12 1 1 2 2 5 5 8 8 11 11 15 15 6 6 10 10 13 13 3 3 4 4 7 7 9 9 12 12 14 14 Start with 1 2,4,3 Stop at 3 Backtrack to 4, can we search? 7,6 Stop at 2 Backtrack to 6, can we search? 10,13 Stop at 13 Backtrack to 10,6,7 9, 11, 8, 5 Stop at 5 Backtrack to 8,11 15,14,12 STOP – All nodes are marked!!

34 Depth First Search – Real Life Application
From xkcd.com

35 Graph Traversal (Contd.)
In both DFS and BFS, the nodes of the undirected graph are visited in a systematic manner so that every node is visited exactly once. Both BFS and DFS consider an embeded tree: When a node x is visited, it is labeled as visited, and it is added to the tree If the traversal got to node x from node y, y is viewed as the parent of x, and x a child of y

36 Depth-First Search DFS follows the following rules:
Select an unvisited node x, visit it, and treat as the current node Find an unvisited neighbor of the current node, visit it, and make it the new current node; If the current node has no unvisited neighbors, backtrack to the its parent, and make that parent the new current node; Repeat steps 3 and 4 until no more nodes can be visited. If there are still unvisited nodes, repeat from step 1.

37 Illustration of DFS 1 2 1 4 9 4 5 7 10 2 11 9 8 5 6 7 11 Graph G 6 8
1 2 1 4 9 4 5 7 10 2 11 9 8 5 6 7 11 Graph G 6 8 10 DFS Tree

38 At seats Write recursive code to do a DFS search

39 Breadth-First Search BFS follows the following rules:
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. 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. Repeat step 2 until no more nodes can be visited. If there are still unvisited nodes, repeat from Step 1.

40 Illustration of BFS 1 2 2 4 1 4 9 5 9 10 5 7 10 11 8 6 7 8 11 6 Graph G BFS Tree

41 Breadth First Search L0: 1 L1: 2, 3 L2: 4,5,6,11 L3: 7,8,10,9,15
13 13 3 3 4 4 7 7 9 9 12 12 14 14 L0: 1 L1: 2, 3 L2: 4,5,6,11 L3: 7,8,10,9,15 L4: 13,12,14 Final output: 1, 2, 3, 4, 5, 6, 11, 7, 8, 10, 9, 15, 13, 12, 14

42 Topological Ordering In college, before taking a particular course, students usually must take all prerequisite courses. For example, before taking the CS II course, students must take the CS I course. However, certain courses can be taken independently of each other. The courses within a department can be represented as a directed graph. A directed edge from, say, vertex u to vertex v means that the course represented by the vertex u is a prerequisite of the course represented by the vertex v. It would be helpful for students to know, before starting a major, the sequence in which they can take courses so that before taking a particular course they take all its prerequisite courses.

43 We assume that the graph has no cycles.
Let G be a directed graph and V(G) = {v1, v2, ..., vn}, where n > 0. A topological ordering of V(G) is a linear ordering vi1, vi2, ..., vin of the vertices such that if vij is a predecessor of vik, j  k, 1  j  n, and 1  k  n, then vij precedes vik, that is, j < k in this linear ordering. We assume that the graph has no cycles. Because the graph has no cycles: There exists a vertex u in G such that u has no predecessor. There exists a vertex v in G such that v has no successor.

44 What algorithm would you use to find a topological order?

45 In the breadth first topological ordering we first find a vertex that has no predecessor vertex and place it first in the topological ordering. We next find the vertex, say v, all of whose predecessors have been placed in the topological ordering and place v next in the topological ordering. To keep track of the number of vertices of a vertex we use the array predCount. Initially, predCount[j] is the number of predecessors of the vertex vj. The queue used to guide the breadth first traversal is initialized to those vertices vk such that predCount[k] is zero.

46 The general algorithm is:

47 The vertices of G3 in breadth first topological ordering are
We illustrate the breadth first topological ordering of the graph G3

48

49

50

51

52

53 Big Picture pseudocode
Look at each edge. Count the number of predecessors of each node. For each node with predCt[node] ==0, put q.enque(node); While (!q.empty()){ curr = q.deque(); cout << curr; // output nodes in topo order for each successor, succ of curr, {predCt[succ]— if predCt[succ]==0) q.enque(succ) }

54 Shortest-Path Algorithms
The input is a weighted graph: associated with each edge (vi, vj) is a cost ci,j. The cost of a path is the sum of the weights of the edges weighted path length The unweighted path length is merely the number of edges on the path.

55 Shortest-Path Algorithms
Single-Source Shortest-Path Problem: Given as input a weighted graph G=(V, E), and a distinguished vertex, s, find the shortest weighted path from s to every other vertex in G.

56 Edge weights Give an example of a graph problem that needs edge weights. What would negative edge weights mean?

57 Negative Cost Cycles In the graph, the shortest path from v1 to v6 has a cost of 6 and the path itself is v1v4v7v6. The shortest unweighted path has 2 edges.

58 Negative Cost Cycles In the graph, we have a negative cost. The path from v5 to v4 has cost 1, but a shorter path exists by following the loop v5v4v2v5v4 which has cost -5. This path is still not the shortest, because we could stay in the loop arbitrarily long.

59 Shortest Path Length: Problems
We will examine 4 algorithms to solve four versions of the problem Unweighted shortest path Weighted shortest path without negative edges Weighted shortest path with negative Weighted shortest path of acyclic graphs

60 Unweighted Shortest Paths
Using some vertex, s, which is an input parameter, find the shortest path from s to all other vertices in an unweighted graph. Assume s=v3. Ideas?

61 Unweighted Shortest Paths
Algorithm: find vertices that are at distance 1, 2, ... N-1 by processing vertices in layers (breadth-first search)

62 Queue contains pairs: (node, distance from source)
void PrintShortestPaths( int source) Queue q; q.enqueue(Pair(source,0)); while(!q.isEmpty()) { Pair p = dequeue(); if (!V[p.node].isVisited) {V[p.node].isVisited = true; cout << “distance from “ << source << “ to “ << p.node << “ is “ <<p.dist; for (Edge * e = V[p.node].adj; e!= NULL; e = e->next) p.enqueue( Pair(e->toNode, p.dist+1)); } // if } // PrintShortestPath

63 What if you wanted the actual path printed?

64 Unweighted Shortest Paths-Implementation
For each vertex v The distance from s in the entry dv A variable to keep the previous node  path A boolean variable to check is the vertex is processed or not  visited class Vertex { List adj; bool visited; int dist; char path; }

65 What if the edges had weights
What if the edges had weights? Weighted Shortest Path Dijkstra’s Algorithm Add the edgewt from node to its successor to the current distance Pull nodes from collection based on shortest distance – using a priority queue

66 1 function Dijkstra(Graph, source):
2 for each vertex v in Graph: // Initializations dist[v] := infinity // Unknown distance function previous[v] := undefined // stores the best predecessor 5 dist[source] := // Distance from s to s 6 Q := copy(Graph) // Set of all unvisited vertices 7 while Q is not empty: // The main loop node := extract_min(Q) // Remove best vertex from PQ for each neighbor succ of node: newDist = dist[node] + length(node, succ) if newDist < dist[succ] // Relax (node,succ) dist[succ] := newDist previous[succ] := node

67 Weighted Shortest Path Dijkstra’s Algorithm
With weighted shortest path, the initial distance dv is tentative. It turns out to be the shortest path length from s to v using only known vertices as intermediates. Greedy algorithm: proceeds in stages doing the best at each stage. Dijkstra’s algorithm selects a vertex v with smallest dv among all unknown vertices and declares it known. Remainder of the stage consists of updating the values dw for all edges (v, w).

68

69 Graphs with negative edge costs
Dijkstra’s algorithm does not work with negative edge costs. Once a vertex u is known, it is possible that from some other unknown vertex v, there is a path back to u that is very negative. Algorithm:. Forget about the concept of known vertices.

70 Graphs with negative edge costs
Dijkstra’s algorithm does not work with negative edge costs. Idea – What if we added a large enough constant to every edge weight so that no edge is negative?

71 Graphs with negative edge costs
Dijkstra’s algorithm does not work with negative edge costs. Idea – What if we added a large enough constant to every edge weight so that no edge is negative? Note, this doesn’t work as now the cost of a path depends on number of edges of path.

72 What to do? We have to ignore the idea of a “known” vertex – since previously known vertices may get a smaller value. We put each node we need to consider on a queue. We begin by placing the start vertex on the queue. At each stage, we remove a node from the queue. We examine the length to all successors. For any successors whose distance has decreased, we change the distance and place that node on the queue. We stop when all nodes have been removed. If we have negative cycles, we will be in an infinite loop – so we need to check for that.

73 Graphs with negative edge costs - I
O(|E|*|V|) Each vertex can dequeue at most O(|V|) times. (Why? Algorithm computes shortest paths with at most 0, 1, ..., |V|-1 edges in this order). Hence, the result! If negative cost cycles, then each vertex should be checked to have been dequeued at most |V| times.

74 Acyclic Graphs If the graph is known to be acyclic, the order in which vertices are declared known, can be set to be the topological order. Running time = O(|V|+|E|) This selection rule works because when a vertex is selected, its distance can no longer be lowered, since by topological ordering rule it has no incoming edges emanating from unknown nodes.

75 What if we need all pairs shortest path?
We could use single-source shortest path repeatedly, but is there a better way?

76 The weight matrix and the graph
Algorithms The weight matrix and the graph 1 v1 v2 3 9 5 1 3 2 v5 3 2 v4 v3 4 CS333 / class 22

77 Algorithms The subproblems How can we define the shortest distance di,j in terms of “smaller” problems? One way is to restrict the paths to only include vertices from a restricted subset. Initially, the subset is empty. Then, it is incrementally increased until it includes all the vertices. CS333 / class 22

78 The basic algorithm for all pairs shortest path is as follows:
Floyd Warshall Algorithm for k = 1 to v for i = 1 to v if path[i,k] != infinity // no path for j = 1 to v path[i,j] = min(path[i,j], path[i,k]+path[k,j]) For me, it is obvious that the algorithm never finds bad path lengths – as it merely replaces the current length from i to j with a path through another node (k) if it is shorter.

79 The weight matrix and the graph
Algorithms The weight matrix and the graph 1 v1 v2 3 9 5 1 3 2 v5 3 2 v4 v3 4 CS333 / class 22

80 P is previous node to use
Algorithms Example 4 5 2 -3 1 3 W = D0 = 1 5 4 2 3 1 2 3 -3 2 P = P is previous node to use CS333 / class 22

81 k = 1 Does anyone want to go through node 1?
Algorithms 4 5 2 -3 1 3 1 2 3 5 -3 4 D0 = k = 1 Does anyone want to go through node 1? 4 5 2 7 -3 1 3 D1[2,3] = min( D0[2,3], D0[2,1]+D0[1,3] ) = min (, 7) = 7 D1[3,2] = min( D0[3,2], D0[3,1]+D0[1,2] ) = min (-3,) = -3 D1 = 1 2 3 P = CS333 / class 22

82 k = 2 Vertices 1, 2 can be intermediate
Algorithms 4 5 2 7 -3 1 3 1 2 3 5 -3 4 D1 = k = 2 Vertices 1, 2 can be intermediate 7 4 5 2 7 -1 -3 1 3 D2[1,3] = min( D1[1,3], D1[1,2]+D1[2,3] ) = min (5, 4+7) = 5 D2[3,1] = min( D1[3,1], D1[3,2]+D1[2,1] ) = min (, -3+2) = -1 D2 = 1 2 3 P = CS333 / class 22

83 k = 3 Vertices 1, 2, 3 can be intermediate
Algorithms 4 5 2 7 -1 -3 1 3 1 2 3 5 -3 4 -1 D2 = k = 3 Vertices 1, 2, 3 can be intermediate 2 7 2 5 7 -1 -3 1 3 D3[1,2] = min(D2[1,2], D2[1,3]+D2[3,2] ) = min (4, 5+(-3)) = 2 D3[2,1] = min(D2[2,1], D2[2,3]+D2[3,1] ) = min (2, 7+ (-1)) D3 = 3 1 2 P = CS333 / class 22

84 Printing intermediate nodes on shortest path from q to r
Algorithms Printing intermediate nodes on shortest path from q to r 3 1 2 interPath(index q, r) if (P[ q, r ]!=0) interPath (q, P[q, r]) println(P[q, r]) interPath (P[q, r], r) return; //no intermediate nodes else return Print q Print interPath(q,r) Print r P = 1 2 3 5 -3 4 CS333 / class 22

85 Algorithms Example CS333 / class 22

86 The final distance matrix and P
Algorithms The final distance matrix and P The values in parenthesis are the non zero P values. CS333 / class 22

87 The call tree for Path(1, 4)
Algorithms The call tree for Path(1, 4) Path(1, 4) Path(6, 4) Path(1, 6) Print v6 P(1, 6)=0 Path(6, 3) Print v3 Path(3, 4) P(3, 4)=0 P(6, 3)=0 The intermediate nodes on the shortest path from 1 to 4 are v6, v3. The shortest path is v1, v6, v3, v4. CS333 / class 22


Download ppt "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."

Similar presentations


Ads by Google