Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234.

Similar presentations


Presentation on theme: "Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234."— Presentation transcript:

1 Graphs Discrete Structure CS203

2 Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234 1 2 3 4

3 Matrices

4 Adjacency Matrix -Directed Multigraphs Can easily generalize to directed multigraphs by putting in the number of edges between vertices, instead of only allowing 0 and 1: For a directed multigraph G = (V,E ) define the matrix A G by: Rows, Columns –one for each vertex in V Value at i th row and j th column is The number of edges with source the i th vertex and target the j th vertex

5 Adjacency Matrix Representation for a Directed Graph 1 2 3 4 5 1234512345 0 1 0 0 1 0 0 1 1 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 5 2 4 3

6 Adjacency Matrix -Directed Multigraphs A: 1 2 34

7 Adjacency Matrix -General A: Notice that answer is symmetric. 12 34

8 Counting Paths Let A be the Adjacency Matrix. What is A 2 ? d c b e a

9 Adjacency-list representation for a directed graph. 1 5 1 2 2 5 4 4 3 3 25 534 4 5 5 Variation: Can keep a second list of edges coming into a vertex.

10 10 Paths Q: Find a longest possible simple path in the following graph: 12 34 e1e1 e3e3 e2e2 e4e4 e5e5 e6e6 e7e7

11 Paths in Directed Graphs Q: Consider digraph adjacency matrix: 1. Find a path from 1 to 4. 2. Is there a path from 4 to 1?

12 Euler Paths and Circuits An Euler path in a graph G is a simple path containing every edge in G. An Euler circuit (or Euler cycle) is a cycle which is an Euler path. NOTE: The definition applies both to undirected as well as directed graphs of all types. NOTE: the graph is Euler circuit ten every node is even degree

13 Hamilton Paths and Circuits A Hamilton path in a graph G is a path which visits ever vertex in G exactly once. A Hamilton circuit (or Hamilton cycle) is a cycle which visits every vertex exactly once, except for the first vertex, which is also visited at the end of the cycle. NOTE: Again, the definition applies both to undirected as well as directed graphs of all types.

14 Hamilton Paths and Cycles a path or cycle that contain every vertex Unlike Euler circuit, there is no known necessary and sufficient condition for a graph to be Hamiltonian. ab c d e f g h i There is a Hamilton path, but no Hamilton cycle.

15 15 Shortest Path Problems We can assign weights to the edges of graphs, for example to represent the distance between cities in a railway network: ChicagoToronto New York Boston 600 700 200 650

16 Shortest Path Problems Such weighted graphs can also be used to model computer networks with response times or costs as weights. What is the shortest path between two vertices in the graph, that is, the path with the minimal sum of weights along the way? This corresponds to the shortest train connection or the fastest connection in a computer network.

17 Dijkstra’s Algorithm procedure Dijkstra(G: weighted connected simple graph with vertices a = v 0, v 1, …, v n = z and positive weights w(v i, v j ), where w(v i, v j ) =  if {v i, v j } is not an edge in G) for i := 1 to n L(v i ) :=  L(a) := 0 S :=  {the labels are now initialized so that the label of a is zero and all other labels are , and the distinguished set of vertices S is empty}

18 Dijkstra’s Algorithm while z  S begin u := the vertex not in S with minimal L(u) S := S  {u} for all vertices v not in S if L(u) + w(u, v) < L(v) then L(v) := L(u) + w(u, v) {this adds a vertex to S with minimal label and updates the labels of vertices not in S} end {L(z) = length of shortest path from a to z}

19 Dijkstra’s Algorithm Example: abdz ec 4 2 1 5 8 10 2 6 30    Step 0

20 Dijkstra’s Algorithm Examp le: abdz ec 4 2 1 5 8 10 2 6 30    Step 1 4 (a) 2 (a)

21 Dijkstra’s Algorithm Exampl e: abdz ec 4 2 1 5 8 10 2 6 30    Step 2 4 (a) 2 (a) 3 (a, c) 10 (a, c) 12 (a, c)

22 Dijkstra’s Algorithm Example : abdz ec 4 2 1 5 8 10 2 6 30    Step 3 4 (a) 2 (a) 3 (a, c) 10 (a, c) 12 (a, c) 8 (a, c, b)

23 Dijkstra’s Algorithm Example : abdz ec 4 2 1 5 8 10 2 6 30    Step 4 4 (a) 2 (a) 3 (a, c) 10 (a, c) 12 (a, c) 8 (a, c, b) 10 (a, c, b, d) 14 (a, c, b, d)

24 Dijkstra’s Algorithm Example : abdz ec 4 2 1 5 8 10 2 6 30    Step 5 4 (a) 2 (a) 3 (a, c) 10 (a, c) 12 (a, c) 8 (a, c, b) 10 (a, c, b, d) 14 (a, c, b, d) 13 (a, c, b, d, e)

25 Dijkstra’s Algorithm Example : abdz ec 4 2 1 5 8 10 2 6 30    Step 6 4 (a) 2 (a) 3 (a, c) 10 (a, c) 12 (a, c) 8 (a, c, b) 10 (a, c, b, d) 14 (a, c, b, d) 13 (a, c, b, d, e)

26 The Traveling Salesman Problem The traveling salesman problem is one of the classical problems in computer science. A traveling salesman wants to visit a number of cities and then return to his starting point. Of course he wants to save time and energy, so he wants to determine the shortest path for his trip. The problem then is to find the circuit of minimum total weight that visits each vertex exactly once.

27 The Traveling Salesman Problem Example: What path would the traveling salesman take to visit the following cities? ChicagoToronto New York Boston 600 700 200 650550 700 Solution: The shortest path is Boston, New York, Chicago, Toronto, Boston (2,000 miles).

28 28 Trees Definition: A tree is a connected undirected graph with no simple circuits. Since a tree cannot have a simple circuit, a tree cannot contain multiple edges or loops. Therefore, any tree must be a simple graph. Theorem: An undirected graph is a tree if and only if there is a unique simple path between any of its vertices.

29 29 Trees Example: Are the following graphs trees? No. Yes. Yes. No.

30 Tree Terminology If v is a vertex in a rooted tree other than the root, the parent of v is the unique vertex u such that there is a directed edge from u to v. When u is the parent of v, v is called the child of u. Vertices with the same parent are called siblings. The ancestors of a vertex other than the root are the vertices in the path from the root to this vertex, excluding the vertex itself and including the root.

31 Tree Terminology The descendants of a vertex v are those vertices that have v as an ancestor. A vertex of a tree is called a leaf if it has no children. Vertices that have children are called internal vertices. If a is a vertex in a tree, then the subtree with a as its root is the subgraph of the tree consisting of a and its descendants and all edges incident to these descendants.

32 Trees Example I: Family tree James ChristineBob FrankJoycePetra

33 Trees Example III: Arithmetic expressions  +- yzxy This tree represents the expression (y + z)  (x - y).

34 Binary Search Trees If we want to perform a large number of searches in a particular list of items, it can be worthwhile to arrange these items in a binary search tree to facilitate the subsequent searches. A binary search tree is a binary tree in which each child of a vertex is designated as a right or left child, and each vertex is labeled with a key, which is one of the items. When we construct the tree, vertices are assigned keys so that the key of a vertex is both larger than the keys of all vertices in its left subtree and smaller than the keys of all vertices in its right subtree.

35 Spanning Trees Definition: Let G be a simple graph. A spanning tree of G is a subgraph of G that is a tree containing every vertex of G. Note: A spanning tree of G = (V, E) is a connected graph on V with a minimum number of edges (|V| - 1). Example: Since winters in Boston can be very cold, six universities in the Boston area decide to build a tunnel system that connects their libraries.

36 Spanning Trees The complete graph including all possible tunnels:BrandeisHarvard MIT Tufts BU UMass The spanning trees of this graph connect all libraries with a minimum number of tunnels.

37 Spanning Trees Example for a spanning tree:BrandeisHarvardMIT Tufts BU UMass Since there are 6 libraries, 5 tunnels are sufficient to connect all of them.

38 Coloring graph To be reported

39 Application to Ad Hoc Networking Networks can be represented by graphs The mobile nodes are vertices The communication links are edges Routing protocols often use shortest path algorithms This lecture is background material to the routing algorithms Vertices Edges

40 Graphs ↔ Networks Graph (Network) Vertexes (Nodes) Edges (Arcs) Flow Communications Telephones exchanges, computers, satellites Cables, fiber optics, microwave relays Voice, video, packets Circuits Gates, registers, processors WiresCurrent Mechanical JointsRods, beams, springsHeat, energy Hydraulic Reservoirs, pumping stations, lakes PipelinesFluid, oil Financial Stocks, currencyTransactionsMoney Transportation Airports, rail yards, street intersections Highways, railbeds, airway routes Freight, vehicles, passengers

41 Network data sets Collaboration graphs Wikipedia, World of Warcraft, Citation graphs Who-talks-to-whom graphs Microsoft IM, Cell phone graphs Information linkage Hyperlinks Technological networks Power grids, communication links, Internet Natural and biological networks Food webs, neural interconnections, cell metabolism

42 Recap A graph consists of nodes and edges Graphs can be directed or undirected, weighted, signed or unweighted Paths between nodes (simple vs. non-simple) Cycles Connectivity Components (and the giant component) Distance (and BFS) Six degrees of separation can be checked with BFS

43 Distance Def: The distance between a pair of nodes is the edge length of the shortest path between them Just number of edges. Can be thought of as all edges having weight of 1 What‘s the distance between MIT and SDC?

44 Distance: Breadth-first search (BFS)

45 Link Failure A B E C D G F Simple rerouting case F detects that link to G has failed F sets a distance of ∞ to G and sends update to A A sets a distance of ∞ to G since it uses F to reach G A receives periodic update from C with 2-hop path to G (via D) A sets distance to G to 3 and sends update to F F decides it can reach G in 4 hops via A

46 Link from A to E fails A advertises distance of ∞ to E B and C had advertised a distance of 2 to E (prior to the link failure) Upon reception of A’s routing update B decides it can reach E in 3 hops; and advertises this to A A decides it can read E in 4 hops; advertises this to C C decides that it can reach E in 5 hops… Link Failure A B E C D G F Routing loop case This behavior is called count-to-infinity


Download ppt "Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234."

Similar presentations


Ads by Google