Relations And Graphs

Relations And Graphs
Slide Note
Embed
Share

Midterm grades are released, and the median was 91%. Grade projections are available for interpretation. An announcement regarding a typo in HW5 and schedule for one-on-one grade discussions. Thanksgiving break schedule changes and upcoming lecture topics like context-free grammars and arithmetic. Details on generating arithmetic expressions and parse trees in context-free grammars.

  • CSE 311
  • Grade Updates
  • Midterm Grades
  • Context-Free Grammars
  • Arithmetic

Uploaded on Feb 18, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Relations And Graphs CSE 311 Autumn 20 Lecture 22

  2. Announcements Midterm grades are out Median was 91% -- you did very well! Even if it did take longer than I intended it to. Also grade projections to interpret your work so far (on Ed). If you want to talk to me one-on-one about grades, I added a few more slots tomorrow. Updated HW5 P6. There was a typo in the find the bug problem If you found the typo, that s a bug. You can explain (just) that bug to get full credit. If you haven t started yet (or you didn t see the typo), there s the bug I intended to put in there still. You can also find that one and explain (just) that bug to get full credit.

  3. Announcements Thanksgiving is on Thursday! So there s no class Thursday or Friday. Wednesday s lecture is wrapping up this slide deck. We ll use the remaining time to talk about common misconceptions from the midterm. Wednesday s polleverywhere will open tonight you most want to talk about? tonight it s what question do

  4. Context Free Grammars A context free grammar (CFG) is a finite set of production rules over: An alphabet of terminal symbols A finite set ? of nonterminal symbols A start symbol (one of the elements of ?) usually denoted ?. A production rule for a nonterminal ? ? takes the form ? ?1?2 |?? Where each ?? ? is a string of nonterminals and terminals.

  5. Arithmetic ? ? + ? ? ? ? ? ? ? 0 1 2 3 4 5 6 7 8|9 Generate 2 ? + ? Generate 2 + 3 4 in two different ways

  6. Arithmetic ? ? + ? ? ? ? ? ? ? 0 1 2 3 4 5 6 7 8|9 Generate 2 ? + ? ? ? + ? ? + ? ? ? + ? 2 ? + ? 2 ? + ? (2 ?) + ? Generate 2 + 3 4in two different ways ? ? + ? ? + ? ? 2 + ? ? 2 + 3 ? 2 + 3 4 ? ? ? ? + ? ? 2 + ? ? 2 + 3 ? 2 + 3 4

  7. Parse Trees Suppose a context free grammar ? generates a string ? A parse tree of ? for ? has Rooted at ? (start symbol) Children of every ? node are labeled with the characters of ? for some ? ? Reading the leaves from left to right gives ?. S 0 S 0 ? 0?0 1?1 0 1 ? 1 S 1 1

  8. Back to the arithmetic ? ? + ? ? ? ? ? ? ? 0 1 2 3 4 5 6 7 8|9 Two parse trees for 2 + 3 4 E E E E E E E E E E E E + E E + 4 E E E E 2 E E 3 3 2 4

  9. How do we encode order of operations If we want to keep in order we want there to be only one possible parse tree. Differentiate between things to add and things to multiply Only introduce a * sign after you ve eliminated the possibility of introducing another + sign in that area. E E E E T T + T T F F T T ? ?|? + ? F F N N F F ? ?|? ? N N ? ? |? 4 N N ? ? ? ? 0 1 2 3 4 5 6 7|8|9 3 2

  10. CNFs in practice Used to define programming languages. Often written in Backus-Naur Form just different notation Variables Variables are <names-in-brackets> like <if-then-else-statement>, <condition>, <identifier> is replaced with = or

  11. BNF for C (no <...> and uses : instead of ::=)

  12. Parse Trees Remember diagramming sentences in middle school? <sentence>::=<noun phrase><verb phrase> <noun phrase>::=<determiner><adjective><noun> <verb phrase>::=<verb><adverb>|<verb><object> <object>::=<noun phrase>

  13. Parse Trees <sentence>::=<noun phrase><verb phrase> <noun phrase>::=<determiner><adjective><noun> <verb phrase>::=<verb><adverb>|<verb><object> <object>::=<noun phrase> The old man the boat.

  14. The old man the boat By Jochen Burghardt - Own work, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=92742400

  15. Power of Context Free Languages There are languages CFGs can express that regular expressions can t e.g. palindromes What about vice versa is there a language that a regular expression can represent that a CFG can t? No! Are there languages even CFGs cannot represent? Yes! {0?1?2?3?|?,? 0} cannot be written with a context free grammar.

  16. Takeaways CFGs and regular expressions gave us ways of succinctly representing sets of strings Regular expressions super useful for representing things you need to search for CFGs represent complicated languages like java code with valid syntax After Thanksgiving, we ll talk about how each of these are equivalent to weaker computers. Next time: Two more tools for our toolbox.

  17. Relations and Graphs

  18. Relations Relations A (binary) relation from ? to ? is a subset of ? ? A (binary) relation on ? is a subset of ? ? Wait what? is a relation on . 3 4 is a way of saying 3 relates to 4 (for the relation) (3,4) is an element of the set that defines the relation.

  19. Relations, Examples It turns out, they ve been here the whole time < on is a relation I.e. { ?,? ? < ? and ?,? }. = on is a relation i.e. { ?,? ? = ? and ?,? } For your favorite function ?, you can define a relation from its domain to its co-domain i.e. { ?,? ? ? = ?} ? when squared gives ? is a relation i.e. { ?,? :?2= ?,?,? }

  20. Relations, Examples Fix a universal set ?. is a relation. What s it on? ?(?) The set of all subsets of ?

  21. More Relations ?1= { ?,1 , ?,2 , ?,1 , ?,3 , ?,3 } Is a relation (you can define one just by listing what relates to what) Equivalence mod 5 is a relation. { ?,? ? ? ??? 5 } We ll also say x relates to y if and only if they re congruent mod 5

  22. Properties of relations What do we do with relations? Usually we prove properties about them. Symmetry A binary relation ? on a set ?is symmetric iff for all ?,? ?, [ ?,? ? ?,? ?] = on is symmetric, for all ?,? if ? = ? then ? = ?. is not symmetric on ?(?) 1,2,3 {1,2,3,4} but 1,2,3,4 {1,2,3} Transitivity A binary relation ? on a set ?is transitive iff for all ?,?,? ?, [ ?,? ? ?,? ? ?,? ?] = on is transitive, for all ?,?,? if ? = ? and ? = c then ? = ?. is transitive on ?(?) for any sets ?,?,? if ? ? and ? ? then ? ?. is not a transitive relation 1 {1,2,3}, 1,2,3 ?( 1,2,3 ) but 1 ? 1,2,3 .

  23. Warm up Show that ? ? ??? ? if and only if ? ?(??? ?) ? ? ??? ? ?|(? ?) ?? = ? ? for ? ?( ?) = ? ?(for k ) ?| ? ? ? ?(??? ?) This was a proof that the relation { ?,? ? ? ??? ? } is symmetric! Show that ?%?=(? ?)%? Where ?%? is the unique ? such that ? = ?? + ? for some integer ?. By definition of %, ? = ?? + (?%?) for some integer ?. Subtracting ?, ? ? = ? 1 ? + (?%?). Observe that ? 1 is an integer, and that this is the form of the division theorem for ? ? %?. Since the division theorem guarantees a unique integer, ? ? %? = (?%?) It was actually overkill to show if and only if. Showing just one direction turns out to be enough!

  24. Youve also done a proof of transitivity! You did this proof on HW4. You were showing: | is a transitive relation on +.

  25. More Properties of relations What do we do with relations? Usually we prove properties about them. Antisymmetry A binary relation ? on a set ?is antisymmetric iff for all ?,? ?, [ ?,? ? ? ? ?,? ?] is antisymmetric on Reflexivity A binary relation ? on a set ?is reflexive iff for all ? ?, [ ?,? ?] is reflexive on

  26. Youve proven antisymmetry too! Antisymmetry A binary relation ? on a set ?is antisymmetric iff for all ?,? ?, [ ?,? ? ? ? ?,? ?] You showed | is antisymmetric on + for all ?,? ?, [ ?,? ? b,a ? ? = ?] is equivalent to the definition in the box above The box version is easier to understand, the other version is usually easier to prove.

  27. Fill out the poll everywhere for Activity Credit! Try a few of your own Go to pollev.com/cse311 and login with your UW identity Or text cse311 to 22333 Decide whether each of these relations are Reflexive, symmetric, antisymmetric, and transitive. on ?(?) on > on | on + | on (??? 3) on Symmetry: for all ?,? ?, [ ?,? ? ?,? ?] Antisymmetry: for all ?,? ?, [ ?,? ? ? ? ?,? ?] Transitivity: for all ?,?,? ?, [ ?,? ? ?,? ? ?,? ?] Reflexivity: for all ? ?, [ ?,? ?]

  28. Try a few of your own Symmetry: for all ?,? ?, [ ?,? ? ?,? ?] Antisymmetry: for all ?,? ?, [ ?,? ? ? ? ?,? ?] Decide whether each of these relations are Reflexive, symmetric, antisymmetric, and transitive. on ? ? reflexive, antisymmetric, transitive on reflexive, antisymmetric, transitive > on antisymmetric, transitive | on + reflexive, antisymmetric, transitive | on reflexive, transitive (??? 3) on reflexive, symmetric, transitive Transitivity: for all ?,?,? ?, [ ?,? ? ?,? ? ?,? ?] Reflexivity: for all ? ?, [ ?,? ?]

  29. Two Prototype Relations A lot of fundamental relations follow one of two prototypes: Equivalence Relation A relation that is reflexive, symmetric, and transitive is called an equivalence relation Partial Order Relation A relation that is reflexive, antisymmetric, and transitive is called a partial order

  30. Equivalence Relations Equivalence relations act kinda like equals (mod n) is an equivalence relation. on compound propositions is an equivalence relation. Fun fact: Equivalence relations partition their elements. An equivalence relation ? on ? divides ? into sets ?1, ?? such that. ? (? ?? for some ?) ?,? (?,? ?? for some ? if and only if ?,? ?) ?? ??= for all ? ?

  31. Partial Orders Partial Orders behave kinda like less than or equal to In the sense that they put things in order But it s only kinda like less than it s possible that some elements can t be compared. | on + is a partial order on ?(?) is a partial order ? is a prerequisite of (or-equal-to) ? is a partial order on CSE courses

  32. Why Bother? If you prove facts about all equivalence relations or all partial orders, you instantly get facts in lots of different contexts. If you learn to recognize partial orders or equivalence relations, you can get a lot a lot of intuition for new concepts in a short amount of time.

  33. Combining Relations Given a relation ? from ? to ? And a relation ? from ? to ?, The relation ? ? from ? to ? is { ?,? ?[ ?,? ? ?,? ?]} Yes, I promise it s ? ? not ? ? it makes more sense if you think about relations (?,? ? ) and (?,? ? ) But also don t spend a ton of energy worrying about the order, we almost always care about ? ?, where order doesn t matter.

  34. Combining Relations To combine relations, it s a lot easier if we can see what s happening. We ll use a representation of a directed graph

  35. Directed Graphs ? = (?,?) ? is a set of vertices (an underlying set of elements) ? is a set of edges (ordered pairs of vertices; i.e. connections from one to the next). Path ?0,?1, ,?? such that ??,??+1 ? Simple Path: path with all ?? distinct Cycle: path with ?0= ?? (and ? > 0) Simple Cycle: simple path plus edge (??,?0) with ? > 0

  36. Directed Graphs ? = (?,?) ? is a set of vertices (an underlying set of elements) ? is a set of edges (ordered pairs of vertices; i.e. connections from one to the next). Path ?0,?1, ,?? such that ??,??+1 ? Simple Path: path with all ?? distinct Cycle: path with ?0= ?? (and ? > 0) Simple Cycle: simple path plus edge (??,?0) with ? > 0

  37. Directed Graphs ? = (?,?) ? is a set of vertices (an underlying set of elements) ? is a set of edges (ordered pairs of vertices; i.e. connections from one to the next). Path ?0,?1, ,?? such that ??,??+1 ? Simple Path: path with all ?? distinct Cycle: path with ?0= ?? (and ? > 0) Simple Cycle: simple path plus edge (??,?0) with ? > 0

  38. Directed Graphs ? = (?,?) ? is a set of vertices (an underlying set of elements) ? is a set of edges (ordered pairs of vertices; i.e. connections from one to the next). Path ?0,?1, ,?? such that ??,??+1 ? Simple Path: path with all ?? distinct Cycle: path with ?0= ?? (and ? > 0) Simple Cycle: simple path plus edge (??,?0) with ? > 0

  39. Representing Relations To represent a relation ? on a set A, have a vertex for each element of ? and have an edge (?,?) for every pair in ?. Let ? be {1,2,3,4} and ? be { 1,1 , 1,2 , 2,1 , 2,3 , 3,4 } 1 2 3 4

  40. Combining Relations If ? = Compute ? ? i.e. every pair (?,?) with a ? with ?,? ? and ?,? ? ?,? , ?,? , ?,? and ? = { ?,? , ?,? , ?,? } 2 2 1 1 3 3

  41. Combining Relations If ? = Compute ? ? i.e. every pair (?,?) with a ? with ?,? ? and ?,? ? ?,? , ?,? , ?,? and ? = { ?,? , ?,? , ?,? } 2 2 1 1 3 3

  42. Combining Relations Let ? be a relation on ?. Define ?0 as { ?,? ? ?} ??= ?? 1 ? ?,? ?? if and only if there is a path of length ? from ? to ? in ?. We can find that on the graph!

  43. More Powers of ?. For two vertices in a graph, ? can reach ? if there is a path from ? to ?. Let ? be a relation on the set ?. The connectivity relation ? consists of all pairs (?,?) such that ? can reach ? (i.e. there is a path from ? to ? in ?) ? = ?=0 ?? Note we re starting from 0 (the textbook makes the unusual choice of starting from ? = 1).

  44. Whats the point of ? ? is also the reflexive-transitive closure of ?. It answers the question what s the minimum amount of edges I would need to add to ? to make it reflexive and transitive. Why care about that? The transitive-reflexive closure can be a summary of data you might want to precompute it so you can easily check if ? can reach ? instead of recomputing it every time.

  45. Relations and Graphs Describe how each property will show up in the graph of a relation. Reflexive Symmetric Antisymmetric Transitive

  46. Relations and Graphs Describe how each property will show up in the graph of a relation. Reflexive Every vertex has a self-loop (an edge from the vertex to itself) Symmetric Every edge has its reverse edge (going the other way) also in the graph. Antisymmetric No edge has its reverse edge (going the other way) also in the graph. Transitive If there s a length-2 path from ? to ?then there s a direct edge from ? to ?

More Related Content